-3

Can anyone explain in depth why this outputs 9

here is my code in PHP:

$x = 4;
$x = $x+++$x++;
echo $x;
Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Aadil Ali
  • 31
  • 2

1 Answers1

4

Execution goes like this:

$x = $x++ + $x++;    ($x = 4)
$x = 4 + $x++;       ($x = 5)
$x = 4 + 5;          ($x = 6)
$x = 9;

For a more detailed answer of a more complex example in Java, see this answer: Incrementor logic

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247