PHP Incrementing and Decrementing Operators
Description
We can use incrementing and decrementing operators to add one or subtract one from a numeric value.
Difference
There are two types of incrementing and decrementing operators.
Incrementing and decrementing operators do different things, depending on where you place them. The differences are summarized in the following table.
Statement | Name | Meaning |
---|---|---|
++$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 |
The incrementing and decrementing operators can be placed either before or after a variable, and the effect is different depending on where the operator is placed. Here's a code example:
<?PHP/* ww w . j a va2 s . co m*/
$foo = 5;
$bar = $foo++;
print "Foo is $foo\n";
print "Bar is $bar\n";
$counter=1;
$counter--;
echo $counter
?>
The code above generates the following result.