Evaluation of statements
| From: | Matthew Kendall | Date: | Wed, 07 Jun 2000 16:58:49 +0000 |
| Subject: | Evaluation of statements | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-1233@lists.php.net to get a copy of this message | ||
In several places, including some tutorials at www.zend.com, I have seen
example code that checks that a variable is set, then checks for a
particular value, all in one statement, like this:
if (isset($foo) && $foo=="bar" ) {
do something;
}
Is this right? In other languages that I am familiar with the compiler (or
interpreter) does not guarantee the order of execution of the two
conditions. It is free to evaluate them in whatever order it likes.
Furthermore, even if it does evaluate them in the order given, it is free to
evaluate both of them before deciding whether the compound condition is
true. This means that the second condition may be evaluated even if $foo is
not set, which is an error (or at least a warning in PHP). It certainly
defeats the intention of the isset() check.
Does the PHP parser really guarantee order of execution, and guarantee to
stop evaluating the expressions in a condition once the condition is false,
or should the code really be written:
if (isset($foo)) {
if ($foo=="bar) {
do something;
}
}