Why is $a + ++$a == 2?
All the answers explaining why you get 2 and not 1 are actually wrong. According to the PHP documentation, mixing +
and ++
in this manner is undefined behavior, so you could get either 1 or 2. Switching to a different version of PHP may change the result you get, and it would be just as valid.
See example 1, which says:
// mixing ++ and + produces undefined behavior
$a = 1;
echo ++$a + $a++; // may print 4 or 5
Notes:
Operator precedence does not determine the order of evaluation. Operator precedence only determines that the expression
$l + ++$l
is parsed as$l + (++$l)
, but doesn't determine if the left or right operand of the+
operator is evaluated first. If the left operand is evaluated first, the result would be 0+1, and if the right operand is evaluated first, the result would be 1+1.Operator associativity also does not determine order of evaluation. That the
+
operator has left associativity only determines that$a+$b+$c
is evaluated as($a+$b)+$c
. It does not determine in what order a single operator's operands are evaluated.
Also relevant: On this bug report regarding another expression with undefined results, a PHP developer says: "We make no guarantee about the order of evaluation [...], just as C doesn't. Can you point to any place on the documentation where it's stated that the first operand is evaluated first?"
A preincrement operator "++" takes place before the rest of the expression it's in evaluates. So it is actually:
echo $l + ++$l; // (1) + (0+1) === 2
a + b
a = 1
b = ++a
:= 2
Why do you expect something else?
In PHP:
$a = 0;
$c = $a + ++$a;
Operator precedence visualized:
$c = ($a) + (++$a);
Evaluation sequence visualized:
$a = 0; ($a = 0)
$a = 1; (++$a)
$c = $a + $a (1 + 1);
Or written out:
The moment the sum operation is performed, $a
is already 1 because ++$a
has been already evaluated. The ++
operator is evaluated before the +
operator.
For the fun:
$a++ + ++$a
Results in 2, too. However if you compare it as an expression, it's not equal:
$a++ + ++$a == $a + ++$a
Where as
$a++ + ++$a == $a-- + --$a
is "equal".
See Also:
- Order of evaluation in PHP (Sep 2013; by NikiC) (via)
My Evaluation Order in PHP blog post explain this in detail, but here is the basic idea:
- Operator precedence and associativity have nothing to do with evaluation order.
- PHP does not guarantee an evaluation order. The order can change between PHP versions without notice and can also be different depending on the surrounding code.
- "Normally" PHP will evaluate left-to-right, with the exception of accesses to "simple" variables (like
$a
). Accesses to simple variables will be executed after more complex expressions, regardless in which order the expressions actually occur. - In this particular case it means that
++$a
is run first because it is a complex expression and only then the value of$a
is fetched (it is already 1 at this point). So effectively you are summing1 + 1 = 2
. - The reason that simple variables are fetched after complex expressions is the Compiled Variables (CV) optimization. If you disable this optimization, for example by using the
@
error suppression operator, all expressions are evaluated left-to-right, including simple variable fetches. - In this particular case it means that
@($a + ++$a)
will result in1
, because first$a
is fetched (0 at that time) and incremented only after that.
++
is the higher precedence operator, so it gets applied first.
So now l = 1.
So 1 + 1 = 2.