Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics Unit Converter Arithmetic Operators

Obayanju Damilare
Obayanju Damilare
3,383 Points

echo vs var_dump

I tried this and the input was 5 and 7, why isn't it 5 and 6. Thanks in advance. $var = 5;
echo $var++;
echo ++$var;

1 Answer

<?php $var = 5; //here $var is equal to 5

echo $var++; //here value increment by 1 but it returns original value i.e. 5 but $var is now 6

echo ++$var; //here $var is 6 but after increment it return the new value i.e. 7

?>

post increment return the old value after incrementing , pre increment return the new value after incrementing

changed from comment to answer

Obayanju Damilare
Obayanju Damilare
3,383 Points

Hmm.... Thank you. But do you know any use case of the post increment; why would we want to return the old value first?

Erick Lopes
Erick Lopes
670 Points

Obayanju Damilare

We use post increment when we want to increment the value of a variable after that particular line of code has been executed. There are situations in which this is very useful for exemple counters in repetition structures generally use post increment.