In examining a PHP page, I noticed the following code:
for ($n=10; $n>0; --$n) {
//foo;
}
Why would one put the decrement operator before the variable?
In examining a PHP page, I noticed the following code:
for ($n=10; $n>0; --$n) {
//foo;
}
Why would one put the decrement operator before the variable?
PHP supports C-style pre-
and post-
increment and decrement operators.
Note: The increment/decrement operators only affect numbers and strings. Arrays, objects and resources are not affected. Decrementing NULL values has no effect too, but incrementing them results in 1.
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.
An Example:
<?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 Manual: Incrementing/Decrementing Operators
EDIT:
for ($n=10; $n>0; --$n) {
echo "Iterating:" . $n . "<br>";
}
OUTPUT:
Iterating:10
Iterating:9
Iterating:8
Iterating:7
Iterating:6
Iterating:5
Iterating:4
Iterating:3
Iterating:2
Iterating:1
In your example, the very first iteration will have
$n = 10
as the part--$n
is executed at the end of the for-loop.
--$x
and $x--
are different operators. They both decrement the variable by 1, but they return different things.
--$x
: This decrements $x
and returns its new value:
$y = --$x;
// Is equivalent to
// $x = $x-1;
// $y = $x;
$x--
: This decrements $x
and returns its original value:
$y = $x--;
// Is equivalent to
// $y = $x;
// $x = $x - 1;
In a for
, loop it shouldn't make a difference. The value is still being decremented.
they are slightly different... and its a performance thing; try something like
<?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.";