Question

Possible Duplicate:
PHP short circuit lazy evaluation, where is it in the php.net manual?
PHP “or” Syntax

I have seen people using the || operator as program flow control as follows:

function() || die("message");

where die("message"); will run if function() returns false. Furthermore, it seems to only work for die(); and exit(); else the interpreter will throw a "syntax error" message.

I'm wondering what this is called and where can I find documentation for its behaviour.

Was it helpful?

Solution

It's just a boolean OR expression. The usage is taking advantage of a behavior called short cutting, where if the first part of the expression evaluates to true, then the second half isn't evaluated because the OR expression is already true.

OTHER TIPS

It's just logic OR. If function() returns true, then the rest of the expression is not evaluated.

This is due to OR / || being an operator with left precedence (see here: http://www.php.net/manual/en/language.operators.precedence.php) as the left is evaluated to be true, there is no point in evaluating the right side as the expression will always be true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top