PHP中减量运算符的两个版本

PHP中减量运算符的两个版本,php,decrement,Php,Decrement,在检查PHP页面时,我注意到以下代码: for ($n=10; $n>0; --$n) { //foo; } 为什么要将减量运算符放在变量之前?PHP支持C风格的前后递增和递减运算符 注意:递增/递减运算符仅影响数字和字符串。数组、对象和资源不受影响。递减 空值也没有效果,但是增加它们会得到1 例如: <?php echo "<h3>Postincrement</h3>"; $a = 5; echo "Should be 5: " . $a++ .

在检查PHP页面时,我注意到以下代码:

for ($n=10; $n>0; --$n) {
    //foo;
}
为什么要将减量运算符放在变量之前?

PHP支持C风格的前后递增和递减运算符

注意:递增/递减运算符仅影响数字和字符串。数组、对象和资源不受影响。递减 空值也没有效果,但是增加它们会得到1

例如:

<?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";
?>
在您的示例中,第一次迭代的$n=10部分-$n在for循环结束时执行

-$x和$x-是不同的运算符。它们都将变量递减1,但返回的内容不同

-$x:此函数递减$x并返回其新值:

$x-:这将递减$x并返回其原始值:


在for循环中,它不应该有什么区别。该值仍在递减。

它们略有不同。。。这是一种表现;试试像这样的东西

<?php 
for($i=0;$i<100000000;++$i){
    //heating up the cpu (if it uses some power saving feature or whatever)
}
$i=0;
$PreIncrementStart=microtime(true);
for($i=0;$i<100000000;++$i){
    //counting to 100 million, using pre-increment.
}
$PreIncrementEnd=microtime(true);
$i=0;
$PostIncrementStart=microtime(true);
for($i=0;$i<100000000;$i++){
    //counting to 100 million, using post-increment.
}
$PostIncrementEnd=microtime(true);
$PreTime=$PreIncrementEnd-$PreIncrementStart;
$PostTime=$PostIncrementEnd-$PostIncrementStart;
if($PreTime<$PostTime){
    echo "the fastest was pre-increment. (which totally makes sense, it's consistent with c/c++, and it uses fewer opcodes than the post-increment, and the `old` value is not returned, only the new value, so we don't need 2 values (old value AND new value, as the post-increment does)..)";
} else {
    echo "the fastest was post-increment... i am very surprised.";
}
echo "the difference was: ".abs($PreTime-$PostTime)." seconds.";
因为前缀运算符。可能是的重复项
for ($n=10; $n>0; --$n) {
   echo "Iterating:" . $n . "<br>";
}
Iterating:10
Iterating:9
Iterating:8
Iterating:7
Iterating:6
Iterating:5
Iterating:4
Iterating:3
Iterating:2
Iterating:1
$y = --$x;
// Is equivalent to
// $x = $x-1;
// $y = $x;
$y = $x--;
// Is equivalent to
// $y = $x;
// $x = $x - 1;
<?php 
for($i=0;$i<100000000;++$i){
    //heating up the cpu (if it uses some power saving feature or whatever)
}
$i=0;
$PreIncrementStart=microtime(true);
for($i=0;$i<100000000;++$i){
    //counting to 100 million, using pre-increment.
}
$PreIncrementEnd=microtime(true);
$i=0;
$PostIncrementStart=microtime(true);
for($i=0;$i<100000000;$i++){
    //counting to 100 million, using post-increment.
}
$PostIncrementEnd=microtime(true);
$PreTime=$PreIncrementEnd-$PreIncrementStart;
$PostTime=$PostIncrementEnd-$PostIncrementStart;
if($PreTime<$PostTime){
    echo "the fastest was pre-increment. (which totally makes sense, it's consistent with c/c++, and it uses fewer opcodes than the post-increment, and the `old` value is not returned, only the new value, so we don't need 2 values (old value AND new value, as the post-increment does)..)";
} else {
    echo "the fastest was post-increment... i am very surprised.";
}
echo "the difference was: ".abs($PreTime-$PostTime)." seconds.";