Incrementing/Decrementing Operators
Incrementing/Decrementing Operators
PHP supports pre- and post-increment and decrement operators. Those unary operators allow to increment or decrement the value by one.
| Example | Name | Effect | | --- | --- | --- | | ++$a | Pre-increment | Increments by one, then returns . | | $a++ | Post-increment | Returns , then increments by one. | | --$a | Pre-decrement | Decrements by one, then returns . | | $a-- | Post-decrement | Returns , then decrements by one. |
Voorbeeld: 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);
?>
Post-increment:
int(5)
int(6)
Pre-increment:
int(6)
int(6)
Post-decrement:
int(5)
int(4)
Pre-decrement:
int(4)
int(4)
Waarschuwing: > The increment and decrement operators have no effect on values of type . A is emitted as of PHP 8.3.0, because this will implicitly cast the value to in the future.
bool``E_WARNING``intThe decrement operator has no effect on values of type . A is emitted as of PHP 8.3.0, because this will implicitly cast the value to in the future.
null``E_WARNING``intThe decrement operator has no effect on non- . A is emitted as of PHP 8.3.0, because a will be thrown in the future.numeric string
E_WARNING``TypeError
Opmerking: > 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
Waarschuwing: > This feature is soft-deprecated as of PHP 8.3.0. The function should be used instead.
str_increment
It is possible to increment a non-
in PHP. The string must be an alphanumerical ASCII string.
Which increments letters up to the next letter, when reaching the letter
the increment is carried to the value on the left.
For example, turns
into .
numeric stringZ``$a = 'Z'; $a++;``$a``'AA'
Voorbeeld: 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;
}
?>
== Alphabetic strings ==
X
Y
Z
AA
AB
AC
== Alphanumeric strings ==
A9
B0
B1
B2
B3
B4
A09
A10
A11
A12
A13
A14
Waarschuwing: > If the alphanumerical string can be interpreted as a
it will be cast to an or . This is particularly an issue with strings that look like a floating point numbers written in exponential notation. The function does not suffer from these implicit type cast.numeric string
int``float``str_incrementVoorbeeld: Alphanumerical string converted to float
<?php $s = "5d9"; var_dump(++$s); var_dump(++$s); ?>string(3) "5e0" float(6)This is because the value is interpreted as a and cast to the value before being incremented. `"5e0"``float``5.0`