Incrementing/Decrementing Operators

PHP supports pre- and post-increment and decrement operators. Those unary operators allow to increment or decrement the value by one.

Increment/decrement Operators
Example Name Effect
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.

Example #1 Examples of Increment/Decrement

<?php
echo 'Post-increment:', PHP_EOL;
$a = 5;
var_dump($a++);
var_dump($a);

echo 'Pre-increment:', PHP_EOL;
$a = 5;
var_dump(++$a);
var_dump($a);

echo 'Post-decrement:', PHP_EOL;
$a = 5;
var_dump($a--);
var_dump($a);

echo 'Pre-decrement:', PHP_EOL;
$a = 5;
var_dump(--$a);
var_dump($a);
?>

The above example will output:

Post-increment:
int(5)
int(6)
Pre-increment:
int(6)
int(6)
Post-decrement:
int(5)
int(4)
Pre-decrement:
int(4)
int(4)
Warning

The increment and decrement operators have no effect on values of type bool. A E_WARNING is emitted as of PHP 8.3.0, because this will implicitly cast the value to int in the future.

The decrement operator has no effect on values of type null. A E_WARNING is emitted as of PHP 8.3.0, because this will implicitly cast the value to int in the future.

The decrement operator has no effect on non- numeric string. A E_WARNING is emitted as of PHP 8.3.0, because a TypeError will be thrown in the future.

Note:

Internal objects that support overloading addition and/or subtraction can also be incremented and/or decremented. One such internal object is GMP.

PERL string increment feature

Warning

This feature is soft-deprecated as of PHP 8.3.0. The str_increment() function should be used instead. As of PHP 8.5.0, incrementing non-numeric strings emits a deprecation warning.

It is possible to increment a non- numeric string in PHP. The string must be an alphanumerical ASCII string. Which increments letters up to the next letter, when reaching the letter Z the increment is carried to the value on the left. For example, $a = 'Z'; $a++; turns $a into 'AA'.

Example #2 PERL string increment example

<?php
echo '== Alphabetic strings ==' . PHP_EOL;
$s = 'W';
for ($n=0; $n<6; $n++) {
    echo ++$s . PHP_EOL;
}
// Alphanumeric strings behave differently
echo '== Alphanumeric strings ==' . PHP_EOL;
$d = 'A8';
for ($n=0; $n<6; $n++) {
    echo ++$d . PHP_EOL;
}
$d = 'A08';
for ($n=0; $n<6; $n++) {
    echo ++$d . PHP_EOL;
}
?>

The above example will output:

== Alphabetic strings ==
X
Y
Z
AA
AB
AC
== Alphanumeric strings ==
A9
B0
B1
B2
B3
B4
A09
A10
A11
A12
A13
A14
Warning

If the alphanumerical string can be interpreted as a numeric string it will be cast to an int or float. This is particularly an issue with strings that look like a floating point numbers written in exponential notation. The str_increment() function does not suffer from these implicit type cast.

Example #3 Alphanumerical string converted to float

<?php
$s = "5d9";
var_dump(++$s);
var_dump(++$s);
?>

The above example will output:

string(3) "5e0"
float(6)

This is because the value "5e0" is interpreted as a float and cast to the value 5.0 before being incremented.

add a note

User Contributed Notes 2 notes

up
63
hartmut at php dot net
14 years ago
Note that 

$a="9D9"; var_dump(++$a);   => string(3) "9E0"

but counting onwards from there 

$a="9E0"; var_dump(++$a);   => float(10)

this is due to "9E0" being interpreted as a string representation of the float constant 9E0 (or 9e0), and thus evalutes to 9 * 10^0 = 9 (in a float context)
up
0
laurent at d-nada dot com
2 hours ago
Warning to simultaneous positioning and assignment:

<?

$list = [ 1, 11, 21, 31 ];
var_dump($list);
// result : [ 1, 11, 21, 31 ];

$list = [ 1, 11, 21, 31 ];
$idx = 2;
$list[$idx] = -- $list[-- $idx];
var_dump($list);
// result : [ 1, 10, 21, 31 ];

$list = [ 1, 11, 21, 31 ];
$idx = 2;
$list[$idx] = -- $list[$idx --];
var_dump($list);
// result : [ 1, 20, 20, 31 ];

$list = [ 1, 11, 21, 31 ];
$idx = 2;
$list[$idx] = $list[$idx --] --;
var_dump($list);
// result : [ 1, 21, 20, 31 ];

$list = [ 1, 11, 21, 31 ];
$idx = 2;
$list[$idx] = $list[-- $idx] --;
var_dump($list);
// result : [ 1, 11, 21, 31 ];

?>

The first case shift to 11, decrement it, and redundantly assign it to itself. 
Result: one single decrement.

The second case remains on the 21, decrement it, and overwrite the 11. 
Result: one single decrement for two targets

The third case remains on the 21, return it, then shift to 11 and overwrite it, finally decrementing the 21. 
Result: an inversion + a decrementation

The last case shift to to 11, return it for the final assignment, decrement it to 10, and then reassign it to 11. 
Result: null.
To Top