Search code examples
phpoperator-precedencecomparison-operators

OR Preference changing with a return


From what I see operator precedence makes sense in these two examples:

$a = false;
$b = true;
$c = $a || $b;

Here $c is true

$a = false;
$b = true;
$c = $a or $b;

Here $c is false


I understand the reasoning behind it. However the following:

$a = false;
$b = true;
return $a or $b;

Returns true, which puzzles me.

What is the reason for this?


Solution

  • or has lower precedence than =, so this:

    $c = $a or $b;
    

    Becomes this:

    ($c = $a) or $b;
    

    But this doesn't make sense:

    (return $a) or $b;
    

    So you get this:

    return ($a or $b);