+5  A: 

Incrementing / Decrementing Operators

++ increment operator

-- decrement operator

Example    Name             Effect
---------------------------------------------------------------------
++$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.

These can go before or after the variable. Putting this operator before the variable is slightly faster.

If put before the variable, the increment / decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment / decrement operation is done.

For example:

$apples = 10;
for ($i = 0; $i < 10; ++$i)
{
    echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}

Live example

In the case above ++$i is used, since it is faster. $i++ would have the same results.

However, you must use $apples--, since first you want to display the current number of apples, and then you want to subtract one from it.

You can also increment letters in PHP:

$i = "a";
while ($i < "c")
{
    echo $i++;
}

Once z is reached aa is next, and so on.

Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.


Stack Overflow Posts:

Peter Ajtai
The difference is a lot more than just speed... consider $x = 0; $y = $x++; compared with $x = 0; $y = ++$x; and the value of $y in each case
Mark Baker
They are also semantically different.
NullUserException
added the linked Stack Overflow Posts to main list
Gordon
@Mark - I was still editing. I edited in an example to show the semantic difference between the two forms.
Peter Ajtai
+1 for the note that decrementers don't work on characters, only on numbers
Mark Baker