Why it works: if (isset ($ var) && $ var) {

Suppose I have a $ var variable 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 if something is true or not? && $ var "if it is not already installed? Does isset () do something for the if statement. Of course, this should return:

 Notice: Undefined variable:$var 
+4
source share
8 answers

When the first part of the if statement fails, the rest is not evaluated, since the whole statement cannot be true. Only if the isset part is true does execution reach your $ var statement.

This is a standard language feature and is common to most programming languages.

It's called Short Cycle Evaluation, and you can learn more about it on Wikipedia here: http://en.wikipedia.org/wiki/Short-circuit_evaluation

+14
source

This is due to short circuit rating .

This means that if the first parameters for the && operator are false, there is no need to evaluate the second parameter.

+2
source

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 for your code to work. You tried to wrap the if statement as follows:

 if ($var && isset($var)) { // something } 

This will not work.

+1
source

& & is a short circuit operator. This means that the rest of the expression will not be processed if the first half of the expression returns false. Because of this, $ var will never be a process to determine if it is defined or not.

+1
source

Another already pointed out that the && operator is lazy. But there is one more point for this question:

If the variable is not defined in PHP, the interpreter treats it as if it had a null value. PHP doesn't care. And since null itself is treated as boolean false , this becomes a valid (logical) expression.

 <?php if( $var ) { echo "something"; } else { echo "nothing"; } 

will not throw an exception, but simply echo nothing if $var not defined.

+1
source

& & operator closes. This means that in an expression like a && b b will not be evaluated if a is false. So in your example, when isset($var) is false, the && $var ignored.

0
source

This is a general optimization of the interpreter / compiler using && (and), the expression will be evaluated until one part of the expression becomes false. Using || (or) 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 
0
source

In PHP (and many other languages), logical operators use a short circuit rating . This means that if the result of an expression is already determined after evaluating part of the expression, then the rest will no longer be evaluated.

In your example, isset($var) returns false. Since the && operator is defined so that it is true only if all of its subexpressions are true, this means that it cannot be true if the left subexpression is false. Therefore, the right subexpression will not be evaluated (and does not cause an error).

Evaluating short circuits is very useful because you can combine a subexpression that will lead to a runtime error with another that ensures that this does not happen. An example is the example you gave. Other languages ​​often use a similar construct for zero-save constructs, for example. if (foo != null && foo.bar == 1) in Java or C # - foo.bar will throw an exception if foo was null, but the merits of short circuit evaluation ensure that it will never be evaluated.

0
source

All Articles