note 48490 modified in language.control-structures by googleguy

From: Date: Fri, 11 Jan 2013 09:40:19 +0000
Subject: note 48490 modified in language.control-structures by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192923@lists.php.net to get a copy of this message
For the people that know C: php uses operator short circuit evaluation. That means that 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 the first operand evaluates to false ?> This comes in handy for situations like this: <?php if ( file_exists($filename) && filemtime($filename) > time() ) do_something(); // filemtime will never give a file-not-found-error, since php will stop evaluating if file_exists returns false ?> --was-- 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 ?> http://php.net/manual/en/language.control-structures.php

« previous php.notes (#192923) next »