Search code examples
phpsyntaxcontrol-flow

php syntax: program flow control using the || operator


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.


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.