+ 2

HELP! Why does this equal 2 and not 3????

<?php $a = 2; $b = $a++; echo "$b"; ?>

20th Dec 2017, 4:37 AM
Jason Hanna
Jason Hanna - avatar
3 Answers
+ 9
because $b = $a++; mean use the value of a to b first then increase the value of a by 1 ,the b will become 3 if you use ++$a instead of $a++.
20th Dec 2017, 4:45 AM
Leon lit
Leon lit - avatar
+ 3
I see! so $b = $a++; = 2 and $b = ++$a; = 3 THANKYOU!!
20th Dec 2017, 4:48 AM
Jason Hanna
Jason Hanna - avatar
+ 3
$a++ is post incrementation which means the value of a is used first then incremented later so when $b=$a++ $b equals two,but in the next line a is incremented
20th Dec 2017, 5:58 AM
᠌᠌Code X
᠌᠌Code X - avatar