+ 2

Why is this code giving 6 as the answer and not 5?

<?php $x = 2; $y = ++$x; echo $x + $y; ?> //result 6

23rd Nov 2019, 2:12 PM
Allan 🔥STORMER🔥🔥🔥🔥
Allan 🔥STORMER🔥🔥🔥🔥 - avatar
3 Respuestas
+ 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
23rd Nov 2019, 2:17 PM
GAWEN STEASY
GAWEN STEASY - avatar
+ 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.
23rd Nov 2019, 2:18 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 2
Thank you Gawen and Sami now I have a clear understanding
23rd Nov 2019, 2:19 PM
Allan 🔥STORMER🔥🔥🔥🔥
Allan 🔥STORMER🔥🔥🔥🔥 - avatar