Incrementing and Decrementing operators are very widely used in most
programming languages. They allow a PHP programmer to increment and
decrement values as needed. Many times they are used in a loop to keep
track of the index key number in the loop's array. We discuss loops in
detail in later sections.
The following table shows their usage and result logic:
| Name |
Usage |
Result |
| Pre-increment |
++$myVar |
Increments $myVar by one |
| Post-increment |
$myVar++ |
Returns $myVar, then increments $myVar by
one |
| Pre-decrement |
--$myVar |
Decrements $myVar by one, then returns $myVar |
| Post-decrement |
$myVar-- |
Returns $myVar, then decrements $myVar by
one |
Here is a code example of incrementing a variable's value by one:
<?php
$myVar = 3;
// Use Post-increment to increment the value by 1
$myVar++;
// Now display to browser or use its new value in script
echo $myVar;
?>
4