In the following code:
$a = 0 or 1;
$b = 0 || 1;
echo "$a, $b"; // 0, 1
Why does $a
equal zero, I thought or
and ||
were interchangeable in PHP?
What exactly is going on with the or
statement to make it return 0
?
I would have assume both results would have been 1
making it echo 1, 1
.
or
is lower precedence than =
which is lower precedence than ""
So your code is equivalent to:
($a = 0) or 1;
$b = (0 || 1);
See the precedence table in the PHP manual.
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