Sinured: You can do the same thing with logical OR; if the first test is true, the second will never be executed.
<?PHP
if (empty($user_id) || in_array($user_id, $banned_list))
{
exit();
}
?>
Kontroll-Strukturen
Inhaltsverzeichnis
- else
- elseif
- Alternative Syntax für Kontroll-Strukturen
- while
- do-while
- for
- foreach
- break
- continue
- switch
- declare
- return
- require
- include
- require_once
- include_once
Jedes PHP-Skript besteht aus einer Reihe von Anweisungen. Eine Anweisung kann eine Zuweisung, ein Funktionsaufruf, eine Schleife, eine bedingte Anweisung oder ein Befehl sein, der nichts macht (eine leere Anweisung). Jede Anweisung endet gewöhnlich mit einem Semikolon. Darüber hinaus können Anweisungen zu einer Anweisungsgruppe zusammengefasst werden, welche durch geschweifte Klammern begrenzt wird. Eine Anweisungsgruppe selbst ist auch wieder eine Anweisung. Die unterschiedlichen Arten von Anweisungen werden in diesem Kapitel erläutert.
if
Das if-Konstrukt ist eine der wichtigsten Möglichkeiten vieler Programmier-Sprachen, PHP eingeschlossen. Es erlaubt die bedingte Ausführung von Programmteilen. PHP kennt eine if -Struktur, die ähnlich wie in der Programmiersprache C implementiert ist:
<?php
if (ausdr)
Anweisung
?>
Wie im Abschnitt über Ausdrücke beschrieben, wird ausdr auf seinen boolschen Wertinhalt ausgewertet. Wenn ausdr als TRUE ausgewertet wird, führt PHP die Anweisung aus. Falls die Auswertung FALSE ergibt, wird die Anweisung übergangen. Mehr Informationen drüber welche Werte als FALSE ausgewertet werden finden Sie im Abschnitt 'Umwandlung nach boolean'.
Das folgende Beispiel wird a ist größer als b anzeigen, wenn $a größer als $b ist:
<?php
if ($a > $b)
echo "a ist größer als b";
?>
Oft werden Sie die bedingte Ausführung von mehr als einer Anweisung wollen. Selbstverständlich ist es nicht erforderlich, jede Anweisung mit einer if-Bedingung zu versehen. Statt dessen können Sie mehrere Anweisungen in Gruppen zusammenfassen. Der folgende Programm-Code wird zum Beispiel a ist größer als b anzeigen, wenn $a größer als $b ist und danach wird der Wert von $a in $b gespeichert:
<?php
if ($a > $b) {
print "a ist größer als b";
$b = $a;
}
?>
if-Anweisungen können beliebig oft innerhalb anderer if-Anweisungen definiert werden. Das ermöglicht ihnen völlige Flexibilität bei der bedingten Ausführung verschiedenster Programmteile.
Kontroll-Strukturen
29-Aug-2007 12:45
01-Aug-2007 11:59
As mentioned below, PHP stops evaluating expressions as soon as the result is clear. So a nice shortcut for if-statements is logical AND -- if the left expression is false, then the right expression can’t possibly change the result anymore, so it’s not executed.
<?php
/* defines MYAPP_DIR if not already defined */
if (!defined('MYAPP_DIR')) {
define('MYAPP_DIR', dirname(getcwd()));
}
/* the same */
!defined('MYAPP_DIR') && define('MYAPP_DIR', dirname(getcwd()));
?>
08-Mar-2007 07:53
@mongoose643
dougnoel was actually trying to provide a good example of how the if function automatically evaluates to false and goes no further if the first condition in an if expression fails can be used to optimize a script. In his example not everyone is an admin, and not all admins can delete. In his script it is an optimized failsafe to check if someone is an admin and has delete permissions while not wasting script time.
01-Mar-2007 06:03
@simplyduh
>Duh, both are booleans in the above case, and its obvious
>that checking one boolean is better than checking 2.
>if ($has_delete_permissions) would work better :)
I read dougnoel's post. He was providing an example of a BAD example. The last line in his post suggests that you NOT check for both - only the admin. So actually what he meant to check for is whether or not they are an admin. If so then they automatically have delete permissions - therefore it may be possible that no variable named "$has_delete_permissions" exists. The resulting statement would be as follows:
if($is_admin)
{
//Statements to delete stuff go here.
}
05-May-2006 06:29
Further response to Niels:
It's not laziness, it's optimization. It saves CPUs cycles. However, it's good to know, as it allows you to optimize your code when writing. For example, when determining if someone has permissions to delete an object, you can do something like the following:
if ($is_admin && $has_delete_permissions)
If only an admin can have those permissions, there's no need to check for the permissions if the user is not an admin.
26-Dec-2004 07:49
For the people that know C: php is lazy when evaluating expressions. That is, as soon as it knows the outcome, it'll stop processing.
<?php
if ( FALSE && some_function() )
echo "something";
// some_function() will not be called, since php knows that it will never have to execute the if-block
?>
This comes in nice in situations like this:
<?php
if ( file_exists($filename) && filemtime($filename) > time() )
do_something();
// filemtime will never give an file-not-found-error, since php will stop parsing as soon as file_exists returns FALSE
?>
