Last updated on September 27, 2023 by Siraj Ul Haq
Increment and decrement operators are called unary operators because they work on a single operand. They are used to perform various functions, such as increasing or decreasing a single value.
Increment Operator:
The increment operator increases the value in the operand by 1. It can be used for loop control, counters, array indexing, updating values, and creating sequences. Look at the below example,
$number = 1;
$number++;
echo $number;
// output: 2
Do you see that the output of $number
is 2? However, the variable $number is initially assigned the value 1. It is then incremented using the post-increment operator ($number++
), which increases the $number
variable by 1 after utilizing its current value.
Decrement Operator:
The decrement operator decreases the value in the operand by 1. It is the opposite of the increment operator. Similarly, like the increment operator, it can be used for loop control, counters, array indexing, updating values, and creating sequences. Look at the below example,
$number = 5;
$number--;
echo $number;
// output: 4
In this example, we have a variable $number
that starts with a value of 5. We use the decrement operator $number--
, which means we subtract 1 from it. That’s why in the output, we have 4.
Let’s understand the increment and decrement operators with the below table.
Name | Operator | Description |
Pre-increment | ++$a | Increments by 1 into $a variable and then return |
Pre-decrement | --$a | Returns $a variable and then increments by 1 |
Post-increment | $a++ | Returns $a variable and then increments by 1 |
Post-decrement | $a-- | Returns $a variable and then decrement by 1 |
Pre-Increment (++$number):
The Pre-Increment operator adds 1 to the variable and then returns incremented value.
Let’s understand this concept through an example below. Imagine we have a variable $score
, and it’s initially assigned a value of 5 which we will increase through the Pre-Increment operator.
$score = 5;
Now apply a pre-increment operator on it,
$updatedScore = ++$score;
Here, the Pre-Increment operator increases the value of the $score
variable by 1, and then it assigns it to the $updatedScore
variable. Below is the complete code of the example.
$score = 5;
++$score;
echo $score;
// output: 6
Pre-decrement (–$number):
In pre-decrement, the variable’s value decreases before using its current value.
Let’s understand it by an example. Assume that we have a variable $credits
has initially assigned value is 10
.
$credits = 10;
To decrease 1 from it’s value, we apply pre-decrement operator on it.
$updatedCredits = --$credits;
In the above line of code, the pre-decrement operator decrease the value by 1 from the $credits
variable and then assign it to the $updatedCredits
variable. You can check the complete code below.
$credits = 10;
$updatedCredits = --$credits;
echo $updatedCredits;
// output: 9
Post-increment ($number++):
In post-increment, the variable’s value is used first, and then it is increased by 1. Let’s consider the variable $a with a value of 10.
$a = 10;
If we apply a pre-decrement operator,
$b = $a++;
In post-increment, the variable `$a` keeps its current value in `$b`, and then `$a` is increased by 1.
Example of post_increment:
$id = 5;
$id++;
echo "id: ". $id;
// output: id: 6
Explanation of example:
In the above example, The variable `$id` starts at 5. With the post-increment operator `$id++`, it becomes 6, resulting in the output “id: 6.”
In post-decrement, the variable’s value is used first, and then it is decreased by 1. Let’s consider the variable $a with a value of 10.
$a = 10;
If we apply a pre-decrement operator,
$b = $a–;
In post-decrement, the variable `$a` keeps its current value in `$b`, and then `$a` is decreased by 1.
Example of post_decrement:
$id = 5;
$id–;
echo "id: ". $id;
// output: id: 4
Explanation of example:
In the above example, The variable `$id` has assigned value 5. Using the post decrement operator `$id–`, it becomes 4, resulting in the output “id: 4.”
In this article, we explain the increment and decrement operators using examples. We also define the pre and post-increment/decrement operators using a table and provide illustrative examples.