tags:

views:

160

answers:

8

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 should return:

Notice: Undefined variable:$var
+11  A: 

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%5Fevaluation

rikh
To add to what rikh said: notices don't really matter. It'd be much more readable to just have if($var). It will perform identically (because null evaluates to false).
brianreavis
It is called [Short Circuit Evaluation](http://en.wikipedia.org/wiki/Short-circuit_evaluation)
vava
+1  A: 

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.

Colin
+2  A: 

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.

egon
+1  A: 

&& 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.

Douglas Mayle
A: 

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 && $varpart is ignored.

Konamiman
+1  A: 

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.

Dirk
While this is true, it is always better to treat warnings as errors
vava
@vava: Yes, that is certainly true. I don't think that the behaviour explained above is a point of glory for the PHP language, and certainly nothing to be exploited on a regular basis...
Dirk
ed209
vava
A: 

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
Patrick
A: 

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.

nd