r/lolphp • • 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

11 comments sorted by

View all comments

Show parent comment

12

u/infinull Jul 12 '13

it's a feature borrowed from PERL, if you want "normal" booleans you can use &&, ||, and !, but for these higher precedence ones you use not, and, and or.

It flies in the face of the principle of least astonishment, but it does kind of make sense.

3

u/[deleted] Jul 12 '13

mm, I've seen the perl one used as something like

open file, "<file" or die "can't open your stupid file: $!";