Incrementing/Decrementing Operators

PHP supports C-style pre- and post-increment and decrement operators.

Table 10-4. Increment/decrement Operators

examplenameeffect
++$aPre-incrementIncrements $a by one, then returns $a.
$a++Post-incrementReturns $a, then increments $a by one.
--$aPre-decrementDecrements $a by one, then returns $a.
$a--Post-decrementReturns $a, then decrements $a by one.

Here's a simple example script:

  1 
  2 <?php
  3 echo "<h3>Postincrement</h3>";
  4 $a = 5;
  5 echo "Should be 5: " . $a++ . "<br>\n";
  6 echo "Should be 6: " . $a . "<br>\n";
  7 
  8 echo "<h3>Preincrement</h3>";
  9 $a = 5;
 10 echo "Should be 6: " . ++$a . "<br>\n";
 11 echo "Should be 6: " . $a . "<br>\n";
 12 
 13 echo "<h3>Postdecrement</h3>";
 14 $a = 5;
 15 echo "Should be 5: " . $a-- . "<br>\n";
 16 echo "Should be 4: " . $a . "<br>\n";
 17 
 18 echo "<h3>Predecrement</h3>";
 19 $a = 5;
 20 echo "Should be 4: " . --$a . "<br>\n";
 21 echo "Should be 4: " . $a . "<br>\n";
 22 ?>	  
 23