PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

if> <Typ Operatoren
Last updated: Fri, 26 Dec 2008

view this page in

Kontroll-Strukturen

Inhaltsverzeichnis

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> <Typ Operatoren
Last updated: Fri, 26 Dec 2008
 
add a note add a note User Contributed Notes
Kontroll-Strukturen
wintermute
30-Aug-2007 05:45
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();
}
?>
Sinured
02-Aug-2007 04: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()));
?>
dougnoel
06-May-2006 11: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.
niels dot laukens at tijd dot com
27-Dec-2004 02: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
?>

if> <Typ Operatoren
Last updated: Fri, 26 Dec 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites