When using the ++ operator by itself on a variable, ++$var is faster than $var++ and uses slightly less memory (in my experiments). It would seem like this could be optimized in the language during runtime (if $var++ is the only thing in the whole statement, it could be treated as ++$var).
I conducted many tests (I believe to be fair), and here's one of the results:
$i++ took 8.47515535355 seconds and 2360 bytes
++$i took 7.80081486702 seconds and 2160 bytes
Here's my code. If anyone sees a bias in it, tell me. I conducted it many times, each time going through a loop one million iterations and doing each test 10 - 15 times (10 - 15 million uses of the ++ operator).
<?php
ini_set( 'MAX_EXEC_TIME', 120 );
ob_start( );
$num_tests = 10;
$startFirst = $startSecond = $endFirst = $endSecond = $startFirstMemory = $endFirstMemory = $startSecondMemory = $endSecondMemory = $someVal = 0;
$times = array( '$i++' => array( 'time' => 0, 'memory' => 0 ), '++$i' => array( 'total' => 0, 'memory' => 0 ) );
for( $j = 0; $j < $num_tests; ++$j )
{
for( $i = 0, $startFirstMemory = memory_get_usage( ), $startFirst = microtime( true ); $i < 10000000; $i++ ){ $someval = 2; }
$endFirstMemory = memory_get_usage( );
$endFirst = microtime( true );
for( $i = 0, $startSecondMemory = memory_get_usage( ), $startSecond = microtime( true ); $i < 10000000; ++$i ){ $someval = 2; }
$endSecondMemory = memory_get_usage( );
$endSecond = microtime( true );
$times[ '$i++' ][ $j ] = array( 'startTime' => $startFirst, 'endTime' => $endFirst, 'startMemory' => $startFirstMemory, 'endMemory' => $endFirstMemory );
$times[ '++$i' ][ $j ] = array( 'startTime' => $startSecond, 'endTime' => $endSecond, 'startMemory' => $startSecondMemory, 'endMemory' => $endSecondMemory );
}
for( $i = 0; $i < $num_tests; ++$i )
{
$times[ '$i++' ][ 'time' ] += ( $times[ '$i++' ][ $i ][ 'endTime' ] - $times[ '$i++' ][ $i ][ 'startTime' ] );
$times[ '++$i' ][ 'time' ] += ( $times[ '++$i' ][ $i ][ 'endTime' ] - $times[ '++$i' ][ $i ][ 'startTime' ] );
$times[ '$i++' ][ 'memory' ] += ( $times[ '$i++' ][ $i ][ 'endMemory' ] - $times[ '$i++' ][ $i ][ 'startMemory' ] );
$times[ '++$i' ][ 'memory' ] += ( $times[ '++$i' ][ $i ][ 'endMemory' ] - $times[ '++$i' ][ $i ][ 'startMemory' ] );
}
echo 'There were ' . $num_tests . ' tests conducted, here\'s the totals<br /><br />
$i++ took ' . $times[ '$i++' ][ 'time' ] . ' seconds and ' . $times[ '$i++' ][ 'memory' ] . ' bytes<br />
++$i took ' . $times[ '++$i' ][ 'time' ] . ' seconds and ' . $times[ '++$i' ][ 'memory' ] . ' bytes';
ob_end_flush( );
?>
Try it yourself, ;)
Opérateurs d'incrémentation et décrémentation
PHP supporte les opérateurs de pre- et post-incrémentation et décrémentation, comme en langage C.
Note: Les opérateurs d'incrémentation/décrémentation n'affectent pas les valeurs booléennes. La décrémentation des valeurs
NULLn'a également aucun effet, mais leur incrémentation donnera comme résultat 1.
| Exemple | Nom | Résultat |
|---|---|---|
| ++$a | Pre-incrémente | Incrémente $a de 1, puis retourne $a. |
| $a++ | Post-incrémente | Retourne $a, puis incrémente $a de 1. |
| --$a | Pré-décrémente | Décrémente $a de 1, puis retourne $a. |
| $a-- | Post-décrémente | Retourne $a, puis décrémente $a de 1. |
Voici un exemple simple :
<?php
echo '<h3>Post-incrémentation</h3>';
$a = 5;
echo "Devrait valoir 5: " . $a++ . "<br />\n";
echo "Devrait valoir 6: " . $a . "<br />\n";
echo '<h3>Pre-incrémentation</h3>';
$a = 5;
echo "Devrait valoir 6: " . ++$a . "<br />\n";
echo "Devrait valoir 6: " . $a . "<br />\n";
echo '<h3>Post-décrémentation</h3>';
$a = 5;
echo "Devrait valoir 5: " . $a-- . "<br />\n";
echo "Devrait valoir 4: " . $a . "<br />\n";
echo '<h3>Pre-décrémentation</h3>';
$a = 5;
echo "Devrait valoir 4: " . --$a . "<br />\n";
echo "Devrait valoir 4: " . $a . "<br />\n";
?>
PHP suit les conventions de Perl pour la gestion des opérateurs arithmétiques sur les variables de caractères et non pas celle du C. Par exemple, en PHP et en Perl, $a = 'Z'; $a++; transforme $a en 'AA', alors qu'en C, a = 'Z'; a++; transforme a en '[' (la valeur ASCII de 'Z' est 90, la valeur ASCII de '[' est 91). Notez que les variables de caractères peuvent être incrémentées mais pas décrémentées, mais aussi que seuls les caractères ASCII pleins (a-z et A-Z) sont supportés. L'incrémentation/décrémentation d'autres variables de caractères n'a aucun effet, la chaîne originale n'est pas modifiée.
Exemple #1 Opérations arithmétiques sur un caractère
<?php
$i = 'W';
for($n=0; $n<6; $n++) {
echo ++$i . "\n";
}
?>
L'exemple ci-dessus va afficher :
X Y Z AA AB AC
L'incrémentation ou la décrémentation d'un booléen n'a aucun effet.
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.
Note that
$a="9D9"; var_dump(++$a); => string(3) "9E0"
but counting onwards from there
$a="9E0"; var_dump(++$a); => float(10)
this is due to "9E0" being interpreted as a string representation of the float constant 9E0 (or 9e0), and thus evalutes to 9 * 10^0 = 9 (in a float context)
If you have a trailing zero and use the increment, the trailing zero will not remain. Was at least unexpected for me at first, although it's logical if you think about it.
<?php
$start = '01';
$start++;
print $start; //Outputs '2' not '02'
?>
I ran some tests (on PHP 5.3.3) of my own and was surprised to find $i += 1 to be the fastest method of incrementing. Here are the methods fastest to slowest:
$i += 1;
++$i;
$i++;
$i = $i + 1;
BEWARE:
If incrementing an uninitialized variable you will not get an E_NOTICE error. This may caused you to not find issue's like the visibility of a property.
<?php
class a {
private $foo = 1;
}
class b extends a {
public function inc() {
echo ++$this->foo;
}
}
$b = new b;
$b->inc();
?>
Will output 1 and not 2 (if $foo was accessible).
Also no notices are given.
(related to what "Are Pedersen" wrote)
With arrays it can lead to much confusion if your index variable is altered on the right side of the = sign, either with ++|-- or even when passed to a function by reference..
Consider these (PHP 5):
<?php
$A[$a] = ++$a; // [1]=1
$B[++$b] = ++$b; // [1]=2
$C[$c+=0] = ++$c; // [0]=1
?>
In 'A' you have to be aware that PHP evaluates $A[$a] last.
Yet in 'B' and 'C' PHP evaluates the index and saves it in a temporary variable.
You can always force PHP to evaluate a variable without explicitly storing it as a named variable first, with a simple "+=0" like in example 'C'.
Compared to 'A', 'C' gives the more logically expected result, when we expect evaluation occurs left to right.
PHP does evaluate left to right BUT it will attempt to cut down on temporary variables, which can lead to confusing results.
So just be aware and use either behavior to your advantage for the desired functionality.
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"
