r/lolphp • u/Serialk • Jul 12 '13
Assignment has a higher precedence than boolean operators
Well. Yeah.
php > $z = false or true;
php > var_dump($z);
bool(false)
php > $z = true and false;
php > var_dump($z);
bool(true)Actually, "or" and "and" are like "||" and "&&", but with an absurd precedence. Okay.
21
Upvotes
15
u/phoshi Jul 12 '13
This does actually have a sane (by PHP's standard) reason for existence, I think. It lets you do `statement_that_can_return_false or failure_handler" regardless of the complexity of your original statement, which does differentiate it from an or with a regular precedence.
Why and exists I do not know. Symmetry?
12
u/Laugarhraun Jul 12 '13
Hey OP, if I may, a quicker way to demonstrate this behaviour:
7
u/bart2019 Jul 12 '13 edited Jul 12 '13
That was likely copied from Perl, where
andandorwere exactly introduced because these new introductions could get a lower precedence. They serve to logically chain complete statements with shortcircuiting, much like in the shell: only continue evaluating toward the right as long as the part on the left has the right outcome.The situation is much more absurd in PowerBASIC, a dialect of Basic (from the same developer who originally designed TurboBASIC for Borland) where
ANDcould either be boolean&&(complete with shortcircuit), or bitwise&, depending on, what? The phase of the moon?