PHP increment and decrement: prefix versus postfix

In PHP, ++ adds one to a numeric variable and -- subtracts one. Prefix forms yield the new value of the expression. Postfix forms yield the previous value. Both forms update the variable itself.

Compare expression results and final values

Each row starts separately with $n = 5. Save the example as increment.php and run php increment.php. result shows the expression value and n shows the variable after the operation.

Expression Form Expression value $n afterward
++$n Prefix increment 6 6
$n++ Postfix increment 5 6
--$n Prefix decrement 4 4
$n-- Postfix decrement 5 4
<?php
$n = 5;
$result = ++$n;
echo "Prefix increment: result=$result, n=$n", PHP_EOL;
$n = 5;
$result = $n++;
echo "Postfix increment: result=$result, n=$n", PHP_EOL;
$n = 5;
$result = --$n;
echo "Prefix decrement: result=$result, n=$n", PHP_EOL;
$n = 5;
$result = $n--;
echo "Postfix decrement: result=$result, n=$n", PHP_EOL;

$result = $n++ stores 5 in $result and changes $n to 6. With $result = ++$n, both values are 6. Decrement works the same way: $n-- yields 5 and --$n yields 4, while both change $n to 4. PHP Manual — Increment and decrement

Check when a for loop updates its counter

A for loop evaluates its third clause after the body and before the next condition check. When the standalone update’s result is unused, $i++ and ++$i produce the same sequence. Save the example as counter-loop.php and run php counter-loop.php.

<?php
for ($i = 0; $i < 3; $i++) {
    echo $i, PHP_EOL;
}
echo "After loop: $i", PHP_EOL;

for ($remaining = 3; $remaining > 0; --$remaining) {
    echo "Remaining: $remaining", PHP_EOL;
}

The first loop prints 0, 1, and 2 and stops with $i equal to 3. The countdown runs its body for 3, 2, and 1, then stops at 0. Change the first update to ++$i to see the same output. Check the initial value, condition, and direction together to ensure the update advances toward the stopping point. PHP Manual — for

Read and update the variable in separate statements

An expression like $n + $n++ reads and changes the same variable, so the result depends on when the value is read. PHP’s precedence rules specify grouping. They do not generally specify operand evaluation order. Save the separated version as separate-update.php and run php separate-update.php. PHP Manual — Operator precedence

<?php
$n = 5;
$before = $n;
$n++;
$sum = $before + $n;
echo "Sum=$sum, n=$n", PHP_EOL;

$count = 0;
$count += 2;
echo "Count=$count", PHP_EOL;

The example saves 5 in $before, updates $n to 6, and then adds them to get 11. The second part changes $count from 0 to 2 with += 2. Use increment or decrement for a one-unit step, and use += or -= to spell out a larger step.

Distinguish numeric counters from alphanumeric labels

Initialize a numeric counter with an integer. Since PHP 8.3, applying either operator to a bool or decrementing null emits a warning. PHP 8.3 discourages the legacy increment of non-numeric alphanumeric strings. As of PHP 8.5, all non-numeric string increments emit a deprecation warning. PHP Manual — Increment and decrement

For alphanumeric labels, use str_increment() and str_decrement() on PHP 8.3 or later. Save the example as string-steps.php and run php string-steps.php. Its first three lines are aa, z, and z. The functions return a new string, so assign the result explicitly to retain it, as in $label = str_increment($label).

<?php
// PHP >= 8.3
$label = 'z';
echo str_increment($label), PHP_EOL;
echo $label, PHP_EOL;
echo str_decrement('aa'), PHP_EOL;

try {
    str_decrement('a');
} catch (ValueError $e) {
    echo 'The string cannot be decremented further.', PHP_EOL;
}

Both functions accept strings made of ASCII letters and digits. Empty strings and disallowed characters cause a ValueError. str_decrement('a') also throws because there is no previous value, so the last line prints the message from the catch block. PHP Manual — str_increment() PHP Manual — str_decrement()