+ 2
Why is this code giving 6 as the answer and not 5?
<?php $x = 2; $y = ++$x; echo $x + $y; ?> //result 6
3 Answers
+ 6
Allan Khasenye
$X = 2 //initially
$y = ++x //due to pre increment first value increment then store so x become 3 and store in y by which y become 3 too
echo $x +$y // 3 + 3 =6
So output is 6
+ 2
Because ++$x; is preincrement so the value of $x = 3, and that value is save to $y
Now $x and $y both has value 3
So 3+3 yields 6.
+ 2
Thank you Gawen and Sami now I have a clear understanding