What's difference between $i++ and ++$i | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

What's difference between $i++ and ++$i

If any code $i=4 $j=$i++ and $j=++$i.what is the value of $j on both questions.I don't understand this

27th Jul 2019, 5:16 AM
🌠Ratnesh Rakesh Ranjan 🇮🇳
🌠Ratnesh Rakesh Ranjan 🇮🇳 - avatar
4 Answers
+ 8
$i++ is known as post-increment. It increments the value of $i only after assigning the original value of $i to $j first. ++$i is known as pre-increment. It increments the value of $i before assigning the value to $j, so the updated value of $i will be assigned to $j. Hence, $i = 4; $j = $i++; // Now, $i = 5 and $j = 4 $i = 4; $j = ++$i; // Now, $i = 5 and $j = 5 These theories apply in a similar manner for decrementing as well. Hope this helps!
27th Jul 2019, 5:43 AM
Tharinda Nimnajith
Tharinda Nimnajith - avatar
+ 5
Thanks
27th Jul 2019, 6:48 AM
🌠Ratnesh Rakesh Ranjan 🇮🇳
🌠Ratnesh Rakesh Ranjan 🇮🇳 - avatar
+ 3
Yes, corrected.. thanks for pointing out!
27th Jul 2019, 4:37 PM
Tharinda Nimnajith
Tharinda Nimnajith - avatar
+ 2
Shouldn’t the last one be pre instead of post again
27th Jul 2019, 2:16 PM
Michael David
Michael David - avatar