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:
- Preincrement $a
- Calculate sum for $a and $a
- 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