8

The php manual claims that:

$a = 1;
echo ++$a + $a++;

is ambiguous under its grammar, but it seems extremely clear to me. ++$a and $a++ evaluate first, from left to right, so ++$a increments and then returns 2, and $a++ returns 2 and then increments. The sum of 2 + 2 is 4, so it would echo 4. However, The PHP Manual says very clearly that it may print 4 or 5.

Does the php spec not specify that operations will be performed from left to right?

Even if it doesn't enforce that operations will be performed from left to right, in this case, wouldn't it return 4 regardless?

Edit: I reread the page, and it stated that it is determined by each specific operator. + has lowest precedence, and evaluates from left to right, so it seems like my earlier assumption was correct. I still do not understand.

nnythm
  • 3,280
  • 4
  • 26
  • 36
  • I agree, I think it should always print 4. – Matthew Apr 05 '12 at 01:39
  • 2
    possible duplicate of [Why is $a + ++$a == 2?](http://stackoverflow.com/questions/9709818/why-is-a-a-2) – alexisdm Apr 05 '12 at 01:59
  • hmm, I thought it was at first, but it doesn't address why it wouldn't be 4 regardless of which way it was performed--I moved a little too hastily and can't seem to undo my close vote now though. – nnythm Apr 05 '12 at 02:11

2 Answers2

5

++$a let $a be 2, and return 2, $a++ increment $a again, so $a is 3 now, but it return 2.

In the same PHP version, the result is always same. But it may produce different result if PHP version changed. It depends on ++$a and $a++, which one is evaluated first. If $a++ is evaluated first, the result will be 5, otherwise the result will be 4.

Wiseguy
  • 20,522
  • 8
  • 65
  • 81
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • This defines the behavior of `$a` but not why the `echo` might display 4 or 5. – jprofitt Apr 05 '12 at 01:49
  • suppose $a++ is evaluated first. then $a++ evaluates to 1, and then ++$a evaluates to 3, and the sum of 1 and 3 is 4, so it should still never display 5. – nnythm Apr 05 '12 at 02:08
3

Please note that this is just my humble opinion.

I think the idea beneath this result is that none of the aperands has precedence when there's a single operator and that in an operation a variable is kept as a reference instead of being replaced by its result during all the calculation until the last one (plus, in this example). So when it goes from l-r:

$a = 1;
++$a + $a++
operand 1 --> ++$a ==> $a = ++1 = 2
result (where $a = 2) --> 2 + (2++) = 4

whereas otherwise:

$a = 1;
++$a + $a++
operand 2 --> $a++ ==> $a = 1
// new operation on the left side
// so the value gets incremented ==> $a = 2
result (where $a = 2) --> (++2) + 2 = 5

I'm not sure about this, though.

Community
  • 1
  • 1
inhan
  • 7,394
  • 2
  • 24
  • 35
  • +1 [This answer](http://stackoverflow.com/a/5434385/185544) may help further explain this. – Wiseguy Apr 05 '12 at 03:35
  • this sort of makes sense, but I have no idea what kind of hoops you would have had to jump through in order to get this functionality. – nnythm Apr 05 '12 at 17:51