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 trialTy Mckenzie
3,465 PointsWhy does var_dump(a$--); not subtract 1?
$a = 5;
$b = 10;
$a = $a + 1;
var_dump($a); <---- = int(6)
$a++;
var_dump($a); <---- = int(7)
$a--;
var_dump($a); <---- = int(6)
var_dump($a--); <---- = int(6) Why isn't this 5?
var_dump($a); <---- = int(5)
var_dump(--$a); <---- = int(4)
var_dump($a); <---- = int(4)
Console
int(6)
int(7)
int(6)
int(6)
int(5)
int(4)
int(4)
1 Answer
Steven Parker
231,186 PointsIt does subtract, the issue is just when.
When you write --$a
, that's the predecrement operator. It subtracts one before returning the value.
But $a--
is the postdecrement operator, it subtracts one after returning the value.
For more details, see the PHP documentation.
Ty Mckenzie
3,465 PointsTy Mckenzie
3,465 PointsAhhh of course. Thanks!