Initial status:
$a = 0;
$x = $a+++(++$a);
When the post-increment operation of $a++
is evaluated, $a
is still worth 0. We have at this moment:
$x = 0 + (++$a);
$a = 1;
When evaluating the pre-increment operation of ++$a
, $a
is already worth 1. We then:
$x = 0 + 2; // $x = 2
$a = 2;
Note that if you invert the expression the answer will still be 2, but the expression values change:
$a = 0;
$x = ++$a+$a++;
When the pre-increment operation of ++$a
is evaluated, $a
becomes valid 1. We have at this moment:
$x = 1 + $a++;
$a = 1;
When the post-increment operation of $a++
is evaluated, $a
is still worth 1. We then:
$x = 1 + 1; // $x = 2
$a = 2;
Now an example with a larger expression. Because $a+++(++$a+$a+++(++$a))
equals 8?
Initial status:
$a = 0
$x = $a+++(++$a+$a+++(++$a));
When the first post-increment operation of $a++
is evaluated, $a
is still 0. We have:
$x = 0 + (++$a+$a+++(++$a));
$a = 1;
When the next pre-increment operation of ++$a
is evaluated, $a
is already worth 1. We then:
$x = 0 + (2 + $a+++(++$a));
$a = 2;
When the next post-increment operation of $a++
is evaluated, $a
is still worth 2. We therefore have:
$x = 0 + (2 + 2 + (++$a));
$a = 3;
When the next pre-increment operation of ++$a
is evaluated, $a
is already worth 3. We finally:
$x = 0 + 2 + 2 + 4; // $x = 8
$a = 4;