+ 11

What is the output of this code? $x=2; $y=++$x; echo $x+$y;

Why the answer is 6

29th May 2019, 10:55 AM
‎‎‎
11 Answers
+ 10
Garuda first only value of x is changing due to pre increment and due to assignment of value of x in y. y value to be changed by 3 so both value become 3
29th May 2019, 12:30 PM
DishaAhuja
DishaAhuja - avatar
+ 14
Garuda hi, $x = 2; $y = ++$x; So $y = 3 $x = 3 due to pre increment the value of x is change to 3 and this 3 is assigned to $y that's what 3+3=6 6 come as output
29th May 2019, 11:03 AM
DishaAhuja
DishaAhuja - avatar
+ 11
If we used $y=$x++; the answer would be 5.
29th May 2019, 11:50 AM
Sonic
Sonic - avatar
+ 11
So the true answer 5 or 6?
29th May 2019, 12:05 PM
‎‎‎
+ 10
but in my opinion the correct answer is 5, because x=2 and y=x++ so x+y=2+3
29th May 2019, 12:09 PM
‎‎‎
+ 10
Garuda according to your questions in which pre increment the answer will be 6 and according to Sonic comment for using post increment then value become 5
29th May 2019, 12:19 PM
DishaAhuja
DishaAhuja - avatar
+ 10
Value y=3? And so the value of x changes to 3 too?
29th May 2019, 12:25 PM
‎‎‎
+ 10
thank you for your answer DishaAhuja
29th May 2019, 12:38 PM
‎‎‎
+ 7
Answer 6
2nd Jun 2019, 6:20 PM
‎محمد النزهي‎
‎محمد النزهي‎ - avatar
0
salom
12th Jun 2019, 6:30 PM
shohruhjon
- 1
Answer 5
7th Jun 2019, 12:19 PM
mani moorthi
mani moorthi - avatar