Incrementing/Decrementing OperatorsPHP supports pre- and post-increment and decrement operators. Those unary operators allow to increment or decrement the value by one.
Here's a simple example script:
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.
A
PERL string increment featureWarning
This feature is soft-deprecated as of PHP 8.3.0. The str_increment function should be used instead.
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
Example #1 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 #2 Alphanumerical string converted to float
The above example will output: string(3) "5e0" float(6)
This is because the value |