So I have a PHP statement of the following type:
if ($x=function($y) || $z == 50) {
What I see happening is that if $z is 50, $x doesn't get set because the function is never called. Is that really possible? I can (and did) fix this easily, but I guess I am confused that that's what is happening and want to make sure I don't make mistakes like this going forward I tried to find out how OR expressions like this are evaluated. Is there a place I can look to see how php gets "compiled"?
You have operator precedence issue. Check this http://www.php.net/manual/en/language.operators.precedence.php
Because ||
has higher precedence than =
Your expression really looks like this
if (
$x = (
function($y) || ( $z == 50 )
)
)
Instead of (what I think was your intention)
if (
($x = function($y)) || ($z == 50)
)
||
has higher precedence than =
, which means your expression becomes:
$x = (foo($y) || ($z == 50));
This means that $x
will always be either true
or false
. Nothing else.
Try:
if( ($x = foo($y)) || ($z == 50))
Or, more readable:
$x = foo($y);
if( $x || $z == 50)
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