NYCPHP Meetup

NYPHP.org

[nycphp-talk] if statement expression evaluation question

Dan Cech dcech at phpwerx.net
Sat Dec 10 16:51:41 EST 2005


Cliff,

Cliff Hirsch wrote:
> I have been looking through the PHP manual for a definitive answer
> regarding expression evaluation in an if statement, specifically
> regarding the following:
> 
> $array = array('a' => 'alpha', 'b' => 'beta', 'c' => 'charlie');
> if (isset($array['d']) && ($array['d'] === 'delta'))
>   echo 'Its a wonderful life';
> 
> This is ok, but the following will generate a parse error:
> 
> if (($array['d'] === 'delta') && isset($array['d'])) 
> 
> Obviously the first example doesn't evaluate the 2nd expression,
> eliminating the non-existent key parsing error. My question is, can I
> rely on this? Is the precedence and abortion of the balance of the if
> expression guaranteed in this and future releases of PHP?

This is known as 'short-circuit' evaluation, and AFAIK it is definitely 
reliable, because it is the most efficient way to evaluate the statement 
so there would be no reason to modify the behaviour.

http://www.php.net/expressions

Dan



More information about the talk mailing list