Pre/Post Increment, Decrement
We touched on these features in PHP before, now lets go a little deeper into the operation of these functions.
Examples
$index++; //Post-increment, Give the value of index, then increments index by one
++$index; // Pre-increment, increments index by one, then returns the valueof index
$index–; // Post-decrement, Give the value of index, then decreases index by one
–$index; // Pre-drecrement, decreased increments index by one, then returns the value of index
Coded Example
< ?php
echo "Post-increment";
$index = 10;
echo "Index value(10): " . $index++. "\n";
echo "Index value(11): " . $index . "\n";
echo "Pre-increment";
$index = 10;
echo "Index value(11): " . ++$index. "\n";
echo "Index value(11): " . $index . "\n";
echo "Post-decrement";
$index = 10;
echo "Index value(10): " . $index--. "\n";
echo "Index value(9): " . $index . "\n";
echo "
Pre-decrement”;
$index = 10;
echo “Index value(9): ” . –$index. “\n”;
echo “Index value(9): ” . $index . “\n”;
?>
This little features, can come in very hand, so always look for ways to simplify your code and make things easier for yourself. Pre/Post decrement and increment are very useful in for loop and counters.


