Beso Kakulia Beso Kakulia - 11 months ago 85
PHP Question

How does php evaluate the expression $x+++$x++?

Please, take a look at the following expression:

$x = 20;

echo $x+++$x++; // 41

Why is the answer
instead of


Altough I can't find any mention in the PHP docs, I guess PHP evaluates from right, and ++ acts like the same operator in C/C++ (see inc/dec PHP operator docs)

So, the expression:


in fact does (evaluated from the right):

  1. get the value of x → 20
  2. increment the value of x → 21
  3. get the value of x → 21
  4. increment the value of x → 22
  5. sum the values got from 1 and 3 → 41

It is worth mentioning, though, that in this case the same results is obtained even if the expression is evaluated from the left.