$a = 10; $a = $a + $a++; // $a = 21 $a = 10; $a = $a + ++$a; // $a = 22 

Why it happens? What is the calculation algorithm?

  • What do you expect? - dakiesse
  • The algorithm is just that. Post-increment (a ++) returns the value BEFORE adding one, ++ a - after. As a result, the first value is one less. - Sergey Snegirev
  • And that returns $a=10; $a=$a++ + ++$a; $a=10; $a=$a++ + ++$a; in pkhp? Expected 23, or more? Or again depends on the version of PHP? - Vesper
  • As far as I know, in C ++ this situation is undefined behavior. So the question is not so trivial. In a good answer, I would like to see a clear explanation of the order in which the operators are executed (including post-increment) in PHP. - Nick Volynkin

1 answer 1

 $a = 10; $a = $a + $a++; // $a = 21 

$ a ++ - will be executed after +

 $a = 10; $a = $a + ++$a; // $a = 22 

++ $ a - will be executed before +

http://php.net/manual/ru/language.operators.increment.php