2009-11-18 170 views
58

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

回答

69

++$i是预增量,而$i++后增量。

  • 预增量:首先增量变量i然后解除引用。
  • 后增量:去参考,然后递增i

“拿的事实,即PHP 允许您张贴增量($ I ++) 和预增(++ $优势i)。意思 是一样的,只要你不是 写什么都像$ j = $ i ++, 然而预先递增几乎是10% 更快,这意味着你应该 从post-当你有机会时,增加 , 特别是在紧密的循环中,尤其是如果你对于微观优化的 迂腐! - TuxRadar

对于进一步澄清,-递增后在PHP已被记录为存储哪些属性此10%的开销相对于预增量的临时变量。

+0

我在你的报价来源很感兴趣。 10%对我来说似乎很重要 – knittl 2009-11-18 13:41:22

+6

这是一个一般的经验法则,还是PHP的具体规定。 – Zoidberg 2009-11-18 13:42:40

+0

...源在我的答案中列出。我没有检查过自己...我想我可以通过查看PHP的源代码,但... – jldupont 2009-11-18 13:43:07

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

这在技术上是过于简单化 - 想到for循环等。 – 2009-11-18 13:50:12

3

为了解释jldupont的观点:

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

差异是:++$i将增加$i变量并返回更新后的值,而$i++将返回原来的价值,所以加一。

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

它可能是最好的例子说明...

后递增:

$zero = 0; 
$n = $zero++; //$n is zero 

预增量:

$zero = 0; 
$n = ++$zero; //$n is one 
34

++$i是预增量

  1. $i递增
  2. 的新值被返回

$i++是-递增后

  1. $i拷贝到内部临时变量
  2. $i递增
  3. 旧的内部拷贝的值返回$i的值
25

++$i增量$i,但求值为$i+1 $i++增量$i,但计算结果为旧值$i

下面是一个例子:

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

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

有时有使用$i++轻微的性能与成本。看,当你做这样的事情

$a = $i++; 

你真的这样做:

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

简短的回答:

  • 前缀增加值,并返回值增加
  • Postfix增加该值,并在增加值之前返回该值
  • 前缀更快

龙答:如果你想想看一点点,你会如何实现这些你自己,你可能会意识到为什么前缀更快。

const T T::operator ++ (int) // postfix 
    { 
    T orig(*this); 
    ++(*this); // call prefix operator 
    return (orig); 
    } 

避免后缀,除非你有特殊原因不能到:使用前缀真相被告知,后缀实际上是(常)实施。对于复杂的数据类型,速度差异可能相当大。

我实际上在几天前看了这个。Heres my source.

3

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

预递增递增后

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

// shorthand 
$x = $y++; // the same statement using $y 
9
在这种情况下

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

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

是没有区别的:

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) 
*/ 

但:

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) 
*/ 
+0

这很有用,前缀增量似乎没有什么意外。我现在要切换到始终使用前缀增量。 – CMCDragonkai 2015-06-26 13:24:47

0

我运行下面的代码来测试++ $ i是否比$ i ++快10%。我承认,代码没有稳定的结果,但即使如此,我至少应该看到接近10%的数字。我得到的最高值约为4-4.5%。

<?php 

$randomFloat = rand(0, 10)/10; 

$before1 = microtime(true); 

for($i=0; $i <1000000; ++$i){ 
    $rand = (rand(0, 10)/10) * (rand(0, 10)/10); 
} 

$after1 = microtime(true); 
echo 'it took '.($after1-$before1) . ' seconds fot ++$i<br />'; 

$before2 = microtime(true); 

for($i=0; $i <1000000; $i++){ 
    $rand = (rand(0, 10)/10) * (rand(0, 10)/10); 
} 

$after2 = microtime(true); 
echo 'it took '.($after2-$before2) . ' seconds fot $i++<br /><br />'; 

echo '++$i is '.((($after1-$before1)*100)/($after2-$before2)-100).'% faster than $i++'; 
3

定位后增量运算符的主要目的是,像这样的:

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

这是一个非常优雅的方式,如何让周围的一些阵列的迭代。 击穿:

  1. 变量$东西将被分配到与$ I
  2. 索引的数组元素
  3. 变量$我将递增
  4. 迭代是在端部,条件将被检查

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

+0

Upvoted推荐使用前缀,除非后缀是严格需要的。 – developerbmw 2016-05-14 20:36:00

2

这个例子elplains只是

<?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 

?> 
+0

感谢这个简单的例子。我现在知道了。 – Praditha 2018-01-12 07:06:04

相关问题