2

As i know, preincrement is calculated before all other operations, postincrement is calculated after all operations.

In php docs it is said that increment (as i understand exactly pre-increment) has very high precedence. Post-increment should have very low precedence, in fact, lowest one.

+ operator precedence is MUCH MORE lower then ++ one (acording to docs).

So, now the question: In php docs there is an example, that shows that preincrement and postincrement in one expression can vary:

// mixing ++ and + produces undefined behavior
$a = 1;
echo ++$a + $a++; // may print 4 or 5

Why? WTF? I see following scenario an it is very clear:

  1. Preincrement $a
  2. Calculate sum for $a and $a
  3. Post increment $a

I can't understand why it is unpredictable. PHP documentation regarding this subject (and also this example) could be found there: http://www.php.net/manual/en/language.operators.precedence.php

avasin
  • 9,186
  • 18
  • 80
  • 127
  • 1
    [It would always produce `4`](http://3v4l.org/iQdFp) – Baba Jun 21 '13 at 14:21
  • is the same code giving different outputs randomly? the code you listed there will always print 4 as far as I can tell. There shouldn't be any inconsistency – Stephan Jun 21 '13 at 14:26
  • 1
    Voting to reopen. The manual talks about a concrete phenomenon (undefined behavior) and the OP is asking why this is even though all precedences should be pretty clear. Not sure what's wrong with this question. – deceze Jun 21 '13 at 14:33
  • 1
    Precedence refers to how operators are grouped, i.e. it says where all the parentheses would be if you had to use them explicitly (as in Lisp). But it doesn't specify the total _order_ that the operations take place, and this is important when side effects are involved. – Barmar Jun 21 '13 at 14:37
  • 1
    For example: in `f1()*f2()+f3()`, precedence says that the multiplication is done before the addition. But that doesn't mean that `f1` must be called before `f3`. – Barmar Jun 21 '13 at 14:40
  • And ^ that ^ should be posted as the answer... – deceze Jun 21 '13 at 14:42
  • 2
    @andrewsi, Eric Leschinski, Baba, ircmaxell, Hamza: why it is not constructive? It is concrete question regarding very strange example in php official docs. Don't be stupid, guys! – avasin Jun 21 '13 at 14:55
  • `$a = 1; echo ++$a + $a++;` gives 4 for me, that's ok because $a is incremented the first time, added to itself, and only then incremented again. But `$a = 1; echo $a + $a++;` gives 3. I don't understand because the `a++` would happen after the rest. it is as if `$a++` had a precedence more important than `+`. – Anthony Mar 09 '18 at 08:54

1 Answers1

0

It would always produce 4 because the last $a is using Post-increment for it to produce 5 then use Pre-increment like the first $a

$a = 1;
echo ++ $a +  $a++; // 4

And

$a = 1;
echo ++ $a + ++ $a; //  5

Incrementing/Decrementing Operators

Baba
  • 94,024
  • 28
  • 166
  • 217
  • I fully agree with you. But my question is regarding docs. I can't example in php docs :( It is not valid. – avasin Jun 21 '13 at 14:53