Search code examples
phpoperator-keywordoperator-precedence

PHP operator precedence "Undefined order of evaluation"?


http://www.php.net/manual/en/language.operators.precedence.php#example-115

<?php
$a = 1;
echo $a + $a++; // may print either 2 or 3
?>

The example from the php manual doesn't explain very well. Why isn't $a++ evaluated to 2, and then added to 1, so that it always becomes echo 1 + 2 // equals 3? I don't understand how it "may print either 2 or 3". I thought incremental ++ has "higher precedence" than addition +?

In other words, I don't understand why isn't it...

$a = 1;

1) echo $a + $a++;
2) echo 1 + ($a = 1 + 1);
3) echo 1 + (2);
4) echo 3;

Solution

  • Operator precedence in PHP is a mess, and it's liable to change between versions. For that reason, it's always best to use parentheses to group your in-line equations so that there is no ambiguity in their execution.

    The example I usually give when asked this question is to ask in turn what the answer to this equation would be:

    $a = 2;
    $b = 4;
    $c = 6;
    $val = $a++ + ++$b - 0 - $c - -++$a;
    
    echo $val;
    

    :)

    Depending where I run it now, I get anything between 4 and 7, or a parser error.

    This will load $a (1) into memory, then load it into memory again and increment it (1 + 1), then it will add the two together, giving you 3:

    $a = 1;
    $val = $a + ($a++);
    

    This, however, is a parser error:

    $a = 1;
    $val = ($a + $a)++;
    

    Anyway, long story short, your example 2) is the way that most versions will interpret it unless you add parenthesis around ($a++) as in the example above, which will make it run the same way in all PHP versions that support the incrementation operator. :)