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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With