PHP 中的 ++$i 和 $i++ 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1756015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:42:45  来源:igfitidea点击:

What's the difference between ++$i and $i++ in PHP?

phpoperators

提问by Steven

What's the difference between ++$iand $i++in PHP?

PHP++$i$i++PHP 中有什么区别?

回答by jldupont

++$iis pre-increment whilst $i++post-increment.

++$i是前增量而后$i++增量。

  • pre-increment: increment variable ifirst and then de-reference.
  • post-increment: de-reference and then increment i
  • pre-increment:先增加变量i,然后取消引用。
  • 后增量:取消引用然后增量 i

"Take advantage of the fact that PHP allows you to post-increment ($i++) and pre-increment (++$i). The meaning is the same as long as you are not writing anything like $j = $i++, however pre-incrementing is almost 10% faster, which means that you should switch from post- to pre-incrementing when you have the opportunity, especially in tight loops and especially if you're pedantic about micro-optimisations!" - TuxRadar

"利用 PHP 允许您后增量 ($i++) 和预增量 (++$i) 的事实。只要您不编写类似 $j = $i++ 的任何内容,含义是相同的,但是预增量快了近 10%,这意味着当你有机会时,你应该从后增量切换到预增量,尤其是在紧凑的循环中,尤其是当你对微优化很迂腐时!” -燕尾服雷达

For further clarification, post-incrementation in PHP has been documented as storing a temporary variable which attributes to this 10% overhead vs. pre-incrementation.

为了进一步澄清,PHP 中的后增量已被记录为存储一个临时变量,该变量归因于与预增量相比的 10% 开销。

回答by Shalom Craimer

++$iincrements $i, but evaluates to the value of $i+1$i++increments $i, but evaluates to the old value of $i.

++$iincrements $i,但计算为$i+1$i++increments的值$i,但计算为 的旧值$i

Here's an example:

下面是一个例子:

$i = 10;
$a = $i++;
// Now $a is 10, and $i is 11

$i = 10;
$a = ++$i;
// Now $a is 11, and $i is 11

There is sometimes a slight preformance cost for using $i++. See, when you do something like

有时使用$i++. 看,当你做类似的事情时

$a = $i++;

You're really doing this:

你真的这样做:

$temporary_variable = $i;
$i=$i+1;
$a=$temporary_variable;

回答by Gumbo

++$iis pre-incrementation

++$i是预增量

  1. $iis incremented
  2. the new value is returned
  1. $i递增
  2. 返回新值

$i++is post-incrementation

$i++是后增量

  1. the value of $icopied to an internal temporary variable
  2. $iis incremented
  3. the internal copy of the old value of $iis returned
  1. 的值$i拷贝到内部临时变量
  2. $i递增
  3. $i返回旧值的内部副本

回答by Sajad Bahmani

++$i //first increment $i then run line
$i++ //first run line then increment $i 

回答by Sajad Bahmani

in this case there is no difference:

在这种情况下没有区别:

for($i = 0;$i<3;++$i)var_dump $i;
/*
int(0)
int(1)
int(2)
*/
for($i = 0;$i<3;$i++)var_dump $i;
/*
int(0)
int(1)
int(2)
*/

but:

但:

for($i = 0;$i<3; $j = ++$i )var_dump($j);
/*
NULL
int(1)
int(2)
*/
for($i = 0;$i<3; $j = $i++ )var_dump($j);
/*
NULL
int(0)
int(1)
*/

回答by Ashraful Alam

this example elplains simply

这个例子很简单

<?php 

$x = 10;  

echo $x++. ' '.$x;  // the result is 10 and 11

echo '<br>';

$y = 10;

echo ++$y. ' ' .$y; // the result is 11 and 11

// so the  $x++ is not showing +1 at first but the next time
// and the ++y is showing +1 first time but not increasing next

回答by Rubens Farias

Differenceis: ++$iwill increment $ivariable and return updated value, while $i++will return original value, so increment it.

区别在于:++$i会递增$i变量并返回更新的值,而$i++会返回原始值,因此递增它。

$prefix = 1;
$postfix = 1;
echo ++$prefix;   // 2
echo $postfix++;  // 1

回答by Boldewyn

To explain jldupont's point:

解释 jldupont 的观点:

$i = 1;
$x = $i++;
echo $x; // prints 1
$x = ++$i;
echo $x; // prints 3

回答by Michael

Another way of looking at pre and post incrementing is that it's shorthand for combining 2 statements.

查看前后递增的另一种方式是它是组合 2 个语句的简写。

Pre-incrementing

预增

// long form
$y = $y + 1;
$x = $y; // any statement using $y

// shorthand
$x = ++$y; // the same statement using $y

Post-incrementing

后递增

// long form
$x = $y; // any statement using $y
$y = $y + 1;

// shorthand
$x = $y++; // the same statement using $y

回答by user3554809

The main purpose of the post-fix increment operator is usage like this:

后修复增量运算符的主要用途是这样的用法:

while(*condition*)
    $array[$i++] = $something;

This is a very elegant way, how to get around some array iterations. Breakdown:

这是一种非常优雅的方式,如何绕过一些数组迭代。分解:

  1. Variable $something will be assigned to the array element indexed with $i
  2. Variable $i will be incremented
  3. Iteration is at the end, conditionwill be checked
  1. 变量 $something 将分配给以 $i 索引的数组元素
  2. 变量 $i 将递增
  3. 迭代结束,将检查条件

In all other cases, you should use the prefix operator. It makes the code much more clear (You can be sure, that you already work with the incremented value of particular variable).

在所有其他情况下,您应该使用前缀运算符。它使代码更加清晰(您可以肯定,您已经使用了特定变量的递增值)。