tags:

views:

369

answers:

9

What's the difference between ++$i and $i++ in PHP?

+11  A: 

++$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.

jldupont
I’m really interested in the source of your quote. 10 % seems a lot to me
knittl
Is this a general rule of thumb, or is it PHP specific.
Zoidberg
... 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
I wouldn't generalize to some other language myself.
jldupont
-1 because it doesn't explain the way it works.
Antony Carthy
The speed increase of pre-incrementation is PHP specific due to the fact that post-increment creates a temporary variable, creating overhead.
cballou
@knittl Remember that it is 10% of a (one hopes) _very_ quick operation :)
jensgram
+3  A: 
++$i first increment i then run line
$i++ first run line then increment i
SjB
Thats technically an oversimplification - think of a for loop etc.
Antony Carthy
+1  A: 

To explain jldupont's point:

$i = 1;
$x = $i++;
echo $x; // prints 1
$x = ++$i;
echo $x; // prints 3
Boldewyn
A: 

Difference is: ++$i will increment $i variable and return updated value, while $i++ will return original value, so increment it.

$prefix = 1;
$postfix = 1;
echo ++$prefix;   // 2
echo $postfix++;  // 1
Rubens Farias
A: 

It's probably best-illustrated by an example...

Post-increment:

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

Pre-increment:

$zero = 0;
$n = ++$zero; //$n is one
brianreavis
+7  A: 

++$i is pre-incrementation

  1. $i is incremented
  2. the new value is returned

$i++ is post-incrementation

  1. the value of $i copied to an internal temporary variable
  2. $i is incremented
  3. the internal copy of the old value of $i is returned
Gumbo
+1  A: 

++$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;
scraimer
+1  A: 

Short answer:

  • Prefix increases the value and returns the value increased
  • Postfix increases the value and returns the value before it was increased
  • Prefix is faster

Long answer: If you think a little about it, how you would implement those yourself, you will probably realize why prefix is faster. Truth to be told, postfix is actually (often) implemented using prefix:

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

Avoid postfix unless you have a specific reason not to. The difference in speed can be quite a lot for complex datatypes.

I actually looked this up a few days ago. Heres my source.

mizipzor
+2  A: 

Another way of looking at pre and post incrementing is that it's shorthand for combining 2 statements.

Pre-incrementing

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

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

Post-incrementing

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

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