Increment and Decrement Operators in PHP

Increment and decrement operators in PHP are those operators that are used to change the value of an operand (constant or variable) by 1. We generally use these operators in loops, counters, and similar operations.

A unique characteristic of increment and decrement operators is that they operate directly on a variable, not just on the stored value.

Rather than only producing the result of the operation, they also modify the value stored inside the variable. This means that the value of the variable is permanently changed after the operation is performed.

For example:

$x = 10;
$x++; // $x is now 11

Here, the ++ operator doesn’t just return a result, but it actually increments the value of $x itself. The value of $x is changed from 10 to 11. This change is not just temporarily for the operation, but permanently in the variable itself.

Types of Increment and Decrement Operators in PHP


In PHP language, there are two types of increment operators and two types of decrement operators, categorized as follows:

  • Pre-increment and Post-increment
  • Pre-decrement and Post-decrement

Learn increment and decrement operators in PHP.

Pre-increment Operator

The pre-increment operator increases the value of a variable by one before using it in an expression. In pre-increment, the operator (++) is placed before the variable. The general syntax is as:

++$variable;

Note: In pre-increment, the increment is done first, and then the updated value is returned. This means that the value of the variable is increased by one before it is used in any further operations.

Example 1:

<?php
$x = 5;
$y = ++$x; // pre-increment
echo $x;
echo "\n";
echo $y;
?>
Output:
      6
      6

In this example, the value of variable $x is incremented to 6, and then its value is assigned to the variable $y. So, both variables $x and $y will have the same value 6.

Post-increment Operator

The post-increment operator increases the value of a variable by one after using it in an expression. In post-increment, the operator (++) is placed after the variable. The general syntax is as:

$variable++;

Note: In post-increment, the current value is returned first, and then the variable is incremented by one after the value is used.

Example 2:

<?php
$x = 5;
$y = $x++; // post-increment
echo $x;
echo "\n";
echo $y;
?>
Output:
      6
      5

Here, the value of variable $x is assigned to $y first (i.e., 5), and then the value of $x is incremented to 6. As a result, the variable $y retains the old value (5), while the variable $x gets the incremented value 6.

Pre-decrement Operator

The pre-decrement operator decreases the value of a variable by one before using it in an expression. In pre-decrement, the operator (--) is placed before the variable. The general syntax is as:

--$variable; // pre-decrement

In pre-decrement, the decrement is done first, and then the updated value is returned. This means that the value of the variable is decreased by one before it is used in any further operations.

Example 3:

<?php
$x = 15;
$y = --$x; // pre-decrement
echo $x;
echo "\n";
echo $y;
?>
Output:
       14
       14

In this example, the value of variable $x is decremented by one before its value is assigned to the variable $y. Therefore, both variables $x and $y will have the same value 14.

Post-decrement Operator

The post-decrement operator decreases the value of a variable by one after using it in an expression. In post-decrement, the operator (--) is placed after the variable. The general syntax is as:

$variable--; // post-decrement

In post-decrement, the current value is returned first, and then the value of the variable is decremented by one after the value is used.


Example 5:

<?php
$x = 15;
$y = $x--; // post-decrement
echo $x;
echo "\n";
echo $y;
?>
Output:
       14
       15

Here, the original value 15 of variable $x is assigned to the variable $y, and then the value of the variable $x is decremented to 14. As a result, the variable $y keeps the old value (15), while the variable $x gets the updated value 14.

Advanced Examples on PHP Increment and Decrement Operators


Let’s take some advanced examples based on the increment and decrement operators in PHP.

Interestingly, you can also apply the increment and decrement operators to strings in PHP. When you will apply, it changes the string’s character sequence.

Example 6:

<?php
$str = "D";
++$str;
echo $str;
$str++;
echo "\n";
echo $str;
?>
Output:
       E
       F

Example 7:

<?php
$i = 0;
echo $i;
echo "\n";

echo ++$i;
echo "\n";

echo $i;
echo "\n";

echo $i++;
echo "\n";
echo $i;
?>
Output:
      0
      1
      1
      1
      2

The statement echo ++$i; increments the value of $i by 1 before its value is used. After this line, the value of variable $i becomes 1, and it prints 1. The line echo $i++; displays the current value of $i (which is 1) and then increments the value of $i by 1 after its value has been used.

Example 8:

<?php
$x = 40;
$y = 60;
$z = 80;

$a = ++$x;
$b = $y++;
$c = $x + $y++ + ++$z;

echo "x = " . $x;
echo "\n";

echo "y = " . $y;
echo "\n";

echo "z = " . $z;
echo "\n";

echo "a = " . $a;
echo "\n";

echo "b = " . $b;
echo "\n";
echo "c = " . $c;
?>
Output:
      x = 41
      y = 62
      z = 81
      a = 41
      b = 60
      c = 183

In this example, the initial values of variables $x, $y, and $z are 40, 60, and 80, respectively.

1. When the interpreter executes the statement $a = ++$x, it increments the value of $x first by 1 and then displays the value of $x as 41. After incrementing, PHP stores the value of $x into the variable $a. Therefore, the value of variable $a is also displayed as 41 (same as that value of $x).

2. When PHP executes the statement $b = $y++, it stores the current value of $y into a variable $b because the increment operator is in postfix form. Therefore, the value of $b is displayed as 60.

After storing the current value of $y into the variable $b, $y’s value is incremented by 2 because $y is modified two times in the program and then it is assigned to $y. Therefore, the value of variable $y is displayed as 62.

3. When PHP executes the statement $c = $x + $y++ + ++$z, it assigns the value of $x as 41 because the value of $x is now 41 after increment. The value of variable $y is incremented by 1 and then it is assigned as 61 into $y because $y++ is in postfix form.

Similarly, the value of $z is also incremented by 1 and then the value 81 is assigned to the variable $z. The sum of three values, 41 + 61 + 81 will be stored into the variable $c. Thus, the result is 183.

Example 9:

<?php
$a = 1;
++$a;

$b = $a++;
$a--;

$c = --$a;
$x = $a * 10 / ($b - $c);

echo "a = " .$a;
echo "\n";
echo "x = " .$x;
?>
Output:
       a = 1
       x = 10

Key Points to Remember

Here, we have listed the following key points in the table for the increment and decrement operators in PHP language that you should remember.

NameExampleDescription
Pre-increment++$xIncrements $x by 1 and then returns $x.
Post-increment$x++Returns $x and then increments $x by 1.
Pre-decrement--$xDecrements $x by 1, then returns $x.
Post-decrement$x--Returns $x and then decrements $x by 1.