PHP Increment & Decrement
In this page:
$x++; // post-increment: use value, then add 1
++$x; // pre-increment: add 1, then use value
$x--; // post-decrement
--$x; // pre-decrement
Post-Increment Operator
$x++ (post-increment) reads out $x's current value first, and only *after* that read does PHP add 1 to the variable.
If you use the expression's result directly, you get the value from before the increment — the update happens, just invisibly to that particular expression.
Example: Post-Increment Operator
<?php
// Declare `$x`, set to `5`
$x = 5;
// Print `$x++` to the output
echo $x++;
// Print "\n" to the output
echo "\n";
// Print `$x` to the output
echo $x;
?>
Login to try C/C++/Java/PHP code in the editor
Pre-Increment Operator
++$x (pre-increment) adds 1 to the variable first, then returns the already-updated value.
Whether you use pre- or post-increment only matters when you use the expression's *result* somewhere else in the same line — as a standalone statement, $x++ and ++$x behave identically.
Example: Pre-Increment Operator
<?php
// Declare `$x`, set to `5`
$x = 5;
// Print `++$x` to the output
echo ++$x;
// Print "\n" to the output
echo "\n";
// Print `$x` to the output
echo $x;
?>
Login to try C/C++/Java/PHP code in the editor
Post-Decrement Operator
$x-- (post-decrement) mirrors post-increment: it returns the current value, then subtracts 1 afterward. It's the natural choice for a countdown loop where you want to act on the value *before* it decreases on that iteration.
Example: Post-Decrement Operator
<?php
// Declare `$x`, set to `5`
$x = 5;
// Print `$x--` to the output
echo $x--;
// Print "\n" to the output
echo "\n";
// Print `$x` to the output
echo $x;
?>
Login to try C/C++/Java/PHP code in the editor
Pre-Decrement Operator
--$x (pre-decrement) subtracts 1 first and returns the new, already-lower value.
As with pre-increment, the difference from the post- version only shows up when the expression's return value is actually consumed rather than the statement standing alone.
Example: Pre-Decrement Operator
<?php
// Declare `$x`, set to `5`
$x = 5;
// Print `--$x` to the output
echo --$x;
// Print "\n" to the output
echo "\n";
// Print `$x` to the output
echo $x;
?>
Login to try C/C++/Java/PHP code in the editor
Incrementing Characters
PHP has an unusual extra ability: incrementing a string containing letters advances it alphabetically — $letter = a; $letter++; produces b, and incrementing z rolls over to aa, the same way a car odometer carries over a digit.
Example: Incrementing Characters
<?php
// Declare `$letter`, set to 'a'
$letter = 'a';
$letter++;
// Print `$letter . "\n"` to the output
echo $letter . "\n";
// Declare `$letter`, set to 'z'
$letter = 'z';
$letter++;
// Print `$letter` to the output
echo $letter;
?>
Login to try C/C++/Java/PHP code in the editor
- Using
$x = $x++;, where the old value is assigned back and the increment is lost. - Mixing up
$i++and++$iinside larger expressions like$a[$i++], so the value used differs by one. - Expecting
++to work onnullas it does on numbers, when incrementingnullgives1but decrementing it leavesnull.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: