Beso Kakulia Beso Kakulia - 1 year ago 134
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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download