I came across this interesting line in the default index.php file for a Zend Framework project:
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
It seems to be saying "If APPLICATION_PATH is not defined, then go on and define it..."
I'm not aware of this control structure in PHP. It's almost like an 'implied if' or 'if/else'. Can anyone help me out on this?
It is not a control structure - it is just how ||
works. If first operand was evaluated to true
- then second is not being evaluated at all.
http://php.net/manual/en/language.operators.logical.php --- look at the first 4 lines of the sample.
// --------------------
// foo() will never get called as those operators are short-circuit
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());
||
is a short-circuiting operator. If the left-hand operand is true
, the expression as a whole must be true
, so it won't bother evaluating the right-hand operand. You can use &&
in a reverse manner; if the left-hand operand is false
, the expression as a whole must be false
, so the right-hand operand won't be evaluated.
This is a rather idiomatic way to do things in some other languages. I'd usually prefer an explicit if
in PHP though for this case.
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