Question

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;
Was it helpful?

Solution 2

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. :)

OTHER TIPS

It can be either 2 or 3. However in most of the time it will be 3. So why it MIGHT be 2? Because PHP is not describing in which order expressions are evaluated, since it might depends on the PHP version.

Order of evaluation isn't a precedence issue. It has nothing to do with operators. The problem also happens with function calls.

By the way, $a++ returns the old value of $a. In your example, $a++ evaluates to 1, not 2.

In the following example, PHP does not define which subexpression is evaluated first: $a or $a++.

$a = 1;
f($a, $a++); //either f(1,1) or f(2,1)

Precedence is about where you put in parentheses. Order of evaluation can't be changed by parentheses. To fix order of evaluation problems, you need to break the code up into multiple lines.

$a = 1;
$a0 = $a;
$a1 = $a++;
f($a0, $a1); //only f(1,1)

Order of evaluation only matters when your subexpressions can have side-effects on each other: the value of one subexpression can change if another subexpression is evaluated first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top