Lets say I have a variable $var that has not been defined. Why don't I get errors with this statement:
if ( isset($var) && $var ){
// something
} else {
// do something else
}
How can you check whether something is true or not "&& $var" if it is not set yet? Does isset() do something to the if statement. Surely this s开发者_如何学运维hould return:
Notice: Undefined variable:$var
When the first part of the if statement fails, the rest of it is not evaluated, since the entire statement can not be true. Only if the isset
part is true, does the execution reach your $var statement.
This is a standard language feature and is common to most programming languages.
It is called "Short Circuit Evaluation", and you can learn more about it on Wikipedia here : http://en.wikipedia.org/wiki/Short-circuit_evaluation
It's because of short-circuit evaluation.
That means if the first parameters for &&
operator is false there's no need to evaluate the second parameter.
This is because when isset()
returns false
, the rest of the if statement is not evaluated. The order in which the 2 parts of the if statement are entered is so that your code works. Have you tried turning around the if statement like so:
if ($var && isset($var))
{
// something
}
This should fail.
&& is a short-circuit operator. That means that the rest of the expression will not be processed if the first half of the expression returns false. Because of this, $var is never process to see if it's defined or not.
Other have already pointed out, that the &&
operator is lazy. But there is another point to this question:
If a variable is not defined in PHP, the interpreter treats it, as if it had the value null
. PHP doesn't really care. And as null
itself is handled as boolean false
, the above becomes a valid (boolean) expression.
<?php
if( $var ) {
echo "something";
} else {
echo "nothing";
}
will not raise an exception, but simply echo nothing
if $var
is not defined.
The && operator is short-circuiting. This means that in an expression like a && b
, b will not be evaluated if a is false. Thus in your example when isset($var)
is false, the && $var
part is ignored.
It s a common interpreter/compiler optimisation with a &&
(and) the expression will be evaluated until one part of the expression if false. With a ||
(or) the expression will be evaluated until one part is true.
Remember that:
false && ?? => false
true && true => true
true && false => false
true || ?? => true
false || true => true
false || false => false
In PHP (and lots of other languages), the logical operators use short circuit evaluation. That means, if the result of an expression is already determined after evaluating a part of the expression, then the rest won't be evaluated anymore.
In your example isset($var)
returns false. Since the &&
operator is defined such that it is true only if all of its sub-expressions are true, this means that it cannot be true if the left sub-expression is false. Therefore, the right sub-expression won't be evaluated (and does not trigger an error).
Short circuit evaluation is very useful because you can combine a sub-expression that would lead to a runtime error with another one that guarantees that this doesn't happen. An example is the one that you have given. Other languages often use a similar construct for null-save constructs, e.g. if (foo != null && foo.bar == 1)
in Java or C# - foo.bar
would cause an exception if foo were null, but the virtues of short circuit evaluation guarantee that this will never be evaluated.
精彩评论