What's the difference between ++$i and $i++ in PHP?
Solution 1
++$i
is pre-increment whilst $i++
post-increment.
- pre-increment: increment variable
i
first and then de-reference. - post-increment: de-reference and then increment
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
For further clarification, post-incrementation in PHP has been documented as storing a temporary variable which attributes to this 10% overhead vs. pre-incrementation.
Solution 2
++$i
increments $i
, but evaluates to the value of $i+1
$i++
increments $i
, but evaluates to the old value of $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
$a = $i++;
You're really doing this:
$temporary_variable = $i;
$i=$i+1;
$a=$temporary_variable;
Solution 3
++$i
is pre-incrementation
-
$i
is incremented - the new value is returned
$i++
is post-incrementation
- the value of
$i
copied to an internal temporary variable -
$i
is incremented - the internal copy of the old value of
$i
is returned
Solution 4
++$i //first increment $i then run line
$i++ //first run line then increment $i
Solution 5
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
Steven
I'm a headhunter based in Hangzhou, China. I recruit software engineers, language experts, C-suite talents for companies. If you need my headhunting services, you can contact me via steven.tu[at]mipfanyi.com(Please replace "[at]" with "@"). If you want to get a job, you can also send a resume to my email.
Updated on March 01, 2020Comments
-
Steven about 4 years
What's the difference between
++$i
and$i++
in PHP? -
knittl over 14 yearsI’m really interested in the source of your quote. 10 % seems a lot to me
-
Zoidberg over 14 yearsIs this a general rule of thumb, or is it PHP specific.
-
jldupont over 14 years... the source is listed in my answer. I haven't checked it out myself... I guess I could by looking at the source code for PHP though...
-
jldupont over 14 yearsI wouldn't generalize to some other language myself.
-
Antony Carthy over 14 yearsThats technically an oversimplification - think of a for loop etc.
-
Corey Ballou over 14 yearsThe speed increase of pre-incrementation is PHP specific due to the fact that post-increment creates a temporary variable, creating overhead.
-
jensgram over 14 years@knittl Remember that it is 10% of a (one hopes) very quick operation :)
-
happy_marmoset over 10 yearsDoesn't Zend PHP and/or Facebook PHP optimize $i++ to ++$i in for loops? Why some stupid team leads still ask to use ++$i in for loops?
-
Solace almost 9 yearsWhat does "de-reference" mean?
-
Daniel Rotter almost 9 years@Zoidberg I think in many languages it is the same, but some would optimize it, if the return value is not used.
-
developerbmw almost 8 years@happy_marmoset just because you think it should be optimized automatically doesn't mean you should write shitty code. It's just as easy to type ++$i and in fact it makes more sense since you don't need a post-increment at all.
-
developerbmw almost 8 yearsUpvoted for recommending to use prefix unless a postfix is strictly needed.
-
xDaizu almost 8 years+1 for pedantic micro-optimization. Especially painful when I see stuff like
for($i=0; $i<count($list); ++$i)
-
Praditha over 6 yearsThank for the simple example. I understand now.
-
James about 6 yearsThis is the better answer. Generic generalisation of what this does without code examples is pointless. The upvotes on such answers are likely from those who already know how it works and thus think they're great answers.
-
Leigh Bicknell about 6 yearsAfter running benchmarks, in php7.3 using a for loop ++$i is actually 50% faster on average. This is the case regardless of which test is run first ($i++ or ++$i). Don't get me wrong, I still think this is a micro-optimization, I just thought people reading this might like to know.
-
Taylor Vance almost 4 yearsI'm sure there's more to it at a lower level, so this question might be moot. But why would PHP need the temp var? Why not: $a=$i; $i=$i+1;
-
coder14 almost 4 years@Taylor, That's a great question! Try replacing $i with a function call like this:
$a=func()++
and ask yourself how you could rewrite it without the ++ and without calling func() more than once. -
Buttle Butkus about 2 years@TaylorVance try
$i = 0; while ($i < 10) { if($i++ === 6) break; } echo "last i is $i at break statement";
And then try it with++$i
instead of$i++
. It will blow your mind. Basically, with$i++
the comparison to$i
happens before the increment. With++$i
, the increment happens first, then the comparison is made. So the echo will contain a different value for$i
depending.