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.

Here's a simple example script:

<?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)
Увага

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.

Зауваження:

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

Увага

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 Z the increment is carried to the value on the left. For example, $a = 'Z'; $a++; turns $a into 'AA'.

Приклад #1 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
Увага

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.

Приклад #2 Alphanumerical string converted to float

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

Поданий вище приклад виведе:

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 1 note

up
71
hartmut at php dot net
12 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)
To Top