Incrementing/Decrementing OperatorsPHP supports pre- and post-increment and decrement operators. Those unary operators allow to increment or decrement the value by one.
Example #1 Examples of Increment/Decrement 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
The decrement operator has no effect on values
of type null.
A
The decrement operator has no effect on non-
numeric string,
except the empty string, which becomes
PERL string increment featureWarning
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
As of PHP 8.3.0, incrementing empty or non-alphanumeric strings
emits Example #2 PERL string increment example 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 The above example will output: string(3) "5e0" float(6)
This is because the value |