A little help with addition

$x = 3;
$y = $x++ + 3;
echo $y;  // equals 6

echo $y = $x + 3; // equals 7

Here the echo y variable yields a 6. Why not 7?
Yet the last equation equals 7.

What am I missing?

$y = $x++ + 3;

What you’re doing here, is assigning $y the value of $x + 3, and afterwards increment the value of $x by 1.

$y would have returned 7 after this statement if you would have user the following construction:

$y = ++$x + 3;

post-increment $x++
after the operation has finished

pre-increment ++$x
before the operation is carried out

got it, thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service