递增/递减运算符

PHP 支持 C 风格的前/后递增与递减运算符。

注: 递增/递减运算符不影响布尔值。递减 NULL 值也没有效果,但是递增 NULL 的结果是 1

表格 15-6. 递增/递减运算符

例子名称效果
++$a前加$a 的值加一,然后返回 $a。
$a++后加返回 $a,然后将 $a 的值加一。
--$a前减$a 的值减一, 然后返回 $a。
$a--后减返回 $a,然后将 $a 的值减一。

一个简单的示例脚本:

<?php
echo "<h3>Postincrement</h3>";
$a = 5;
echo
"Should be 5: " . $a++ . "<br />\n";
echo
"Should be 6: " . $a . "<br />\n";

echo
"<h3>Preincrement</h3>";
$a = 5;
echo
"Should be 6: " . ++$a . "<br />\n";
echo
"Should be 6: " . $a . "<br />\n";

echo
"<h3>Postdecrement</h3>";
$a = 5;
echo
"Should be 5: " . $a-- . "<br />\n";
echo
"Should be 4: " . $a . "<br />\n";

echo
"<h3>Predecrement</h3>";
$a = 5;
echo
"Should be 4: " . --$a . "<br />\n";
echo
"Should be 4: " . $a . "<br />\n";
?>

在处理字符变量的算数运算时,PHP 沿袭了 Perl 的习惯,而非 C 的。例如,在 Perl 中 'Z'+1 将得到 'AA',而在 C 中,'Z'+1 将得到 '['(ord('Z') == 90,ord('[') == 91)。注意字符变量只能递增,不能递减。

例子 15-4. 涉及字符变量的算数运算

<?php
$i
= 'W';
for (
$n=0; $n<6; $n++) {
    echo ++
$i . "\n";
}
?>

上例将输出:

X
Y
Z
AA
AB
AC

递增或递减布尔值没有效果。


add a note add a note User Contributed Notes
julien-bernie-laurent at polenord.com
01-Mar-2006 11:55
to thus trying to increment a string and are blocked by the exponential typecast explained in the message below, here is a small function :

function increment($var) {
   $var2 = '_'.$var;
   return substr(++$var2,1);
}
timo at frenay dot net
25-Aug-2004 11:45
JMcCarthy AT CitiStreet DOT com:

As for your March 31 post, at least in PHP version 4.3 this no longer holds for 'D'. Your point is still valid for 'e' or 'E' and worth noting.

Your comment from May 12 is simply not true, although it might be a bug in your specific version of PHP but that would seem very strange.

<?php
   $Align
= array('a', 'b', 'c');
  
$i = 0;
   echo
$Align[$i++]; // Prints 'a', as expected
?>

It might be interesting to know that pre-/postincrement assumes a value of 0 for undefined variables, but pre-/postdecrement does not:

<?php
  
echo var_dump(++$foo); // int(1)
  
echo var_dump(--$bar); // NULL!
?>
01-Apr-2004 05:19
Note that incrementing strings can give unpredictable results due to type changes.  For example:

<?php

$i
= '9C6';
for(
$n=0; $n<10; $n++)
  echo ++
$i . "\n";

?>

Gives you:
 9C7
 9C8
 9C9
 9D0
 10
 11
 12
..etc.

The 'D' (and also 'E') characters are interpreted here as exponents of 10 (i.e., scientific notation) formatted numbers.  Using '9D6' will give 9000001, 9000002, etc.

You might want to use all alphabetical or all numerical, but not mix the two otherwise you may not get what you expect..
chris at free-source dot com
07-Feb-2004 08:11
Interesting performance note:

$i++ seems to be slightly slower than ++$i, when used on a line by itself the 2 have the same purpose.  It's not much, but over 100,000 incements the pre-increment is about .004 seconds faster on average.
mu at despammed dot net
15-Oct-2002 12:11
The exact moment when post-increment and post-decrement happen is _just immediately after the variable is evaluated_ (not "after the line is processed" or something like that)

Example 1:
$i = 2;
echo $i++ + $i;
Result: 5. The first i is evaluated as 2, gets incremented to 3. i is then evaluated as 3 for the second occurance.

Example 2:
$i = 2;
echo $i + $i++;
Result: 4. The first i is 2. Second i is 2 too, gets incremented afterwards.
cleong at letstalk dot com
18-Oct-2001 10:52
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.
fred at surleau dot com
19-Jul-2001 03:02
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"