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

Question about post-increment operator

Why does the following code equal 5 and not 6.

$a = 5; var_dump($a++);

Thank you.

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

HI Andrew,

The post-increment operator returns the value first and then will increment it. So, here it returns 5 to the var_dump method call and once that call is complete, only then will the variable increment, so the var_dump() method is never aware of the incrementation, as it happened after the method is called.
If you want it to return the value + 1 to the method, you will need to use the pre-increment operator.

Hope that helps to clear it up.

Keep Coding! :) :dizzy:

Thank you Jason! Definitely helps me understand the order of operation more clearer.