递增/递减运算符

PHP 支持前/后递增与递减运算符。这些一元运算符允许将值递增或递减 1。

递增/递减运算符
示例 名称 效果
++$a 前加 $a 的值加一,然后返回 $a
$a++ 后加 返回 $a,然后将 $a 的值加一。
--$a 前减 $a 的值减一, 然后返回 $a
$a-- 后减 返回 $a,然后将 $a 的值减一。

一个简单的示例脚本:

<?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)
警告

递增和递减运算符对 bool 类型值没有影响。自 PHP 8.3.0 起,会发出 E_WARNING,因为将来会默认将该值转换为 int

递减运算符对 null 类型的值没有影响。自 PHP 8.3.0 起,会发出 E_WARNING,因为将来会默认将该值转换为 int

递减运算符对非数字字符串没有影响。自 PHP 8.3.0 起,会发出 E_WARNING,因为在将来会抛出 TypeError

注意:

支持重载加/减法的内部对象也可以进行递增或递减。其中一个这样的内部对象是 GMP

PERL 字符串递增功能

警告

自 PHP 8.3.0 起,此功能已软弃用。应该使用 str_increment() 函数。

在 PHP 中,可以递增非数字字符串。该字符串必须是字母数字 ASCII 字符串。当到达字母 Z 且递增到下个字母时,将进位到左侧值。例如,$a = 'Z'; $a++;$a 变为 'AA'

示例 #1 PERL 字符串递减功能

<?php
echo '== Alphabetic strings ==' . PHP_EOL;
$s = 'W';
for (
$n=0; $n<6; $n++) {
echo ++
$s . PHP_EOL;
}
// 字母数字字符串的不同行为
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
警告

如果数字字母字符串可以解释为数字字符串,则将转换为 intfloat。这对于看起来像以指数形式写出的浮点数字符串来说尤其是一个问题。str_increment() 函数不会受到这些默认类型转换的影响。

示例 #2 数字字母字符串转换为浮点数

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

以上示例会输出:

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

这是因为值 "5e0" 解释为 float,并在递增之前转换为 5.0

add a note add a note

User Contributed Notes 10 notes

up
60
hartmut at php dot net
11 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
3
ayyappan dot ashok at gmail dot com
7 years ago
Rule for Increment and decrement:

At some moment we could be confused with increment and decrement in various cases. To avoid such cases, let us follow certain logical rule behind to get successful results with out mess.

<?php
           $n
= 3;
           echo
$n-- + --$n;
           echo
"<br/>";
           echo
$n;
?>

1. Postfix form of ++,-- operator follows the rule  [ use-then-change ],

2. Prefix form (++x,--x) follows the rule [ change-then-use ].

Solution based on the rule:

Step 1: 
use then change   $n--  use is 3 and change is 2

Step 2. 
change then use   --$n  change is 2 and use is 1

Step 3.
use + use = (3 + 1) = 4

Courtesy : stackoverflow : Sunil Dhillon : 4686665
up
2
aluciffer at hotmail dot com
9 years ago
Regarding character incrementing and PHP following Perl's convention with character operations.
Actually i found that there is a difference, and incrementing and decrementing unfortunately does not yield the reverse, expected results.
For example, the following piece of code:
<?php
echo '== Alphabets ==' . PHP_EOL;
$s = 'W';
for (
$n=0; $n<10; $n++) {
    echo ++
$s . ' ';
}
echo
PHP_EOL;

for (
$n=10; $n>0; $n--) {
    echo (--
$s) . ' ';
}
?>
Will output:
== Alphabets ==
X Y Z AA AB AC AD AE AF AG
AG AG AG AG AG AG AG AG AG AG

Please note that the decrement operator has no effect on the character or string.

On the other hand, in Perl, the similar script:

#!/usr/bin/perl

my $s = 'W';

foreach (1 .. 10) {
print  ++$s . " ";


print "\n";

foreach (1 .. 10) {
print --$s . " ";
}

Will output:

X Y Z AA AB AC AD AE AF AG
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
up
3
cleong at letstalk dot com
22 years ago
Note that the ++ and -- don't convert a boolean to an int. The following code will loop forever.

function a($start_index) {
for($i = $start_index; $i < 10; $i++) echo "\$i = $i\n";
}

a(false);

This behavior is, of course, very different from that in C. Had me pulling out my hair for a while.
up
0
dsbeam at gmail dot com
14 years ago
When using the ++ operator by itself on a variable, ++$var is faster than $var++ and uses slightly less memory (in my experiments).  It would seem like this could be optimized in the language during runtime (if $var++ is the only thing in the whole statement, it could be treated as ++$var).

I conducted many tests (I believe to be fair), and here's one of the results:

$i++ took 8.47515535355 seconds and 2360 bytes
++$i took 7.80081486702 seconds and 2160 bytes

Here's my code.  If anyone sees a bias in it, tell me.  I conducted it many times, each time going through a loop one million iterations and doing each test 10 - 15 times (10 - 15 million uses of the ++ operator).

<?php

ini_set
( 'MAX_EXEC_TIME', 120 );
ob_start( );

$num_tests = 10;
$startFirst = $startSecond = $endFirst = $endSecond = $startFirstMemory = $endFirstMemory = $startSecondMemory = $endSecondMemory = $someVal = 0;
$times = array( '$i++' => array( 'time' => 0, 'memory' => 0 ), '++$i' => array( 'total' => 0, 'memory' => 0 ) );

for(
$j = 0; $j < $num_tests; ++$j )
{
        for(
$i = 0, $startFirstMemory = memory_get_usage( ), $startFirst = microtime( true ); $i < 10000000; $i++ ){ $someval = 2; }
       
$endFirstMemory = memory_get_usage( );
       
$endFirst = microtime( true );

        for(
$i = 0, $startSecondMemory = memory_get_usage( ), $startSecond = microtime( true ); $i < 10000000; ++$i ){ $someval = 2; }
       
$endSecondMemory = memory_get_usage( );
       
$endSecond = microtime( true );

       
$times[ '$i++' ][ $j ] = array( 'startTime' => $startFirst, 'endTime' => $endFirst, 'startMemory' => $startFirstMemory, 'endMemory' => $endFirstMemory );
       
$times[ '++$i' ][ $j ] = array( 'startTime' => $startSecond, 'endTime' => $endSecond, 'startMemory' => $startSecondMemory, 'endMemory' => $endSecondMemory );
}

for(
$i = 0; $i < $num_tests; ++$i )
{
       
$times[ '$i++' ][ 'time' ] += ( $times[ '$i++' ][ $i ][ 'endTime' ] - $times[ '$i++' ][ $i ][ 'startTime' ] );
       
$times[ '++$i' ][ 'time' ] += ( $times[ '++$i' ][ $i ][ 'endTime' ] - $times[ '++$i' ][ $i ][ 'startTime' ] );
       
$times[ '$i++' ][ 'memory' ] += ( $times[ '$i++' ][ $i ][ 'endMemory' ] - $times[ '$i++' ][ $i ][ 'startMemory' ] );
       
$times[ '++$i' ][ 'memory' ] += ( $times[ '++$i' ][ $i ][ 'endMemory' ] - $times[ '++$i' ][ $i ][ 'startMemory' ] );
}

echo
'There were ' . $num_tests . ' tests conducted, here\'s the totals<br /><br />
$i++ took '
. $times[ '$i++' ][ 'time' ] . ' seconds and ' . $times[ '$i++' ][ 'memory' ] . ' bytes<br />
++$i took '
. $times[ '++$i' ][ 'time' ] . ' seconds and ' . $times[ '++$i' ][ 'memory' ] . ' bytes';

ob_end_flush( );

?>

Try it yourself, ;)
up
-4
Brad Proctor
13 years ago
I ran some tests (on PHP 5.3.3) of my own and was surprised to find $i += 1 to be the fastest method of incrementing.  Here are the methods fastest to slowest:

$i += 1;
++$i;
$i++;
$i = $i + 1;
up
-3
ayyappan dot ashok at gmail dot com
7 years ago
Rule Incrementing or decrementing booleans has no effect.

It has an effort on Incrementing or decrementing booleans.

Please look over the code.

$var = true;
echo ++$var;   //Results 1

Similarly

$var = true;
echo ++$var;   //Results 1

$var = (int)false;
echo ++$var;   //Results 1

$var = (int)false;
echo $var++;   //Results 0;   

Note : Tested on PHP Version 5.5.32
up
-3
leo zandvliet
5 years ago
Please note the difference between post-incrementing in a for-loop and recursive function calls (don't use it in the latter!).

<?php
$increment
= $preIncrement = $postIncrement = 1;
echo
$increment.' - '.$preIncrement.' - '.$postIncrement;
echo
'<br>';
echo (
$increment+1).' - '.(++$preIncrement).' - '.($postIncrement++);
echo
'<br>';
echo (
$increment+1).' - '.(++$preIncrement).' - '.($postIncrement++);
echo
'<br>';
echo (
$increment+1).' - '.(++$preIncrement).' - '.($postIncrement++);
?>

Outputs:
1 - 1 - 1
2 - 2 - 1
2 - 3 - 2
2 - 4 - 3

The for-loop:
<?php
for($i=0; $i<4; $i++)
{
    echo
$i.'<br>';
}
?>

Outputs:
0
1
2
3

And the 'headache' mistake, post-increment as parameter of a recursive call:
<?php
testFunctionNesting
(3, 1, 1, 1);

function
testFunctionNesting($max, $increment, $preIncrement, $postIncrement)
{   
    echo
$increment.' - '.$preIncrement.' - '.$postIncrement;
    echo
'<br>';
   
    if(
$increment>=$max)
    {
       
$inc = $increment;
       
$pre = $preIncrement;
       
$post = $postIncrement;
        return;
    }
       
   
   
testFunctionNesting($max, ($increment+1), (++$preIncrement), ($postIncrement++));
}
?>

Output shows that the postIncremented value is never really available as incremented value:
1 - 1 - 1
2 - 2 - 1
3 - 3 - 1
up
-11
sneskid at hotmail dot com
14 years ago
(related to what "Are Pedersen" wrote)
With arrays it can lead to much confusion if your index variable is altered on the right side of the = sign, either with ++|-- or even when passed to a function by reference..
Consider these (PHP 5):
<?php
$A
[$a] = ++$a; // [1]=1
$B[++$b] = ++$b; // [1]=2
$C[$c+=0] = ++$c; // [0]=1
?>
In 'A' you have to be aware that PHP evaluates $A[$a] last.
Yet in 'B' and 'C' PHP evaluates the index and saves it in a temporary variable.

You can always force PHP to evaluate a variable without explicitly storing it as a named variable first, with a simple "+=0" like in example 'C'.

Compared to 'A', 'C' gives the more logically expected result, when we expect evaluation occurs left to right.
PHP does evaluate left to right BUT it will attempt to cut down on temporary variables, which can lead to confusing results.

So just be aware and use either behavior to your advantage for the desired functionality.
up
-16
fred at surleau dot com
22 years ago
Other samples :
$l="A";      $l++; -> $l="B"
$l="A0";     $l++; -> $l="A1"
$l="A9";     $l++; -> $l="B0"
$l="Z99";    $l++; -> $l="AA00"
$l="5Z9";    $l++; -> $l="6A0"
$l="9Z9";    $l++; -> $l="10A0"
$l="9z9";    $l++; -> $l="10a0"
$l="J85410"; $l++; -> $l="J85411"
$l="J99999"; $l++; -> $l="K00000"
$l="K00000"; $l++; -> $l="K00001"
To Top