PHP Incrementing and Decrementing Operators

In this chapter you will learn:

  1. What are incrementing and decrementing operators
  2. Difference between 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 NameMeaning
++$aPre-incrementIncrements $a by one, then returns $a
$a++Post-increment Returns $a, then increments $a by one
--$aPre-decrementDecrements $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/*from  j a  v a2s  . c o 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.

Next chapter...

What you will learn in the next chapter:

  1. What are Comparison Operators
  2. What are PHP Comparison Operators
Home » PHP Tutorial » PHP Operators
PHP Assignment Operators
PHP Arithmetic Operators
PHP Incrementing and Decrementing Operators
PHP Comparison Operators
PHP Logical Operators
PHP String Operators
PHP Ternary Operator
PHP Bitwise Operators
PHP Execution Operator
PHP Operator Precedence