What are the PHP Increment and Decrement Operators?

Last updated on October 2, 2023

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.

NameOperatorDescription
Pre-increment++$aIncrements by 1 into $a variable and then return
Pre-decrement--$aReturns $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 the 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;
$updatedScore = ++$score;
echo $updatedScore;
// output: 6
Pre increment operator in PHP

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 that has initially assigned a value that is 10.

$credits = 10;

To decrease 1 from its value, we apply a pre-decrement operator to it.

$updatedCredits = --$credits;

In the above line of code, the pre-decrement operator decreases the value by 1 from the $credits variable and then assigns it to the $updatedCredits variable. You can check the complete code below.

$credits = 10;
$updatedCredits = --$credits;
echo $updatedCredits;
// output: 9
Pre decrement operator in PHP

Post-increment ($number++):

The post-increment operator is used to increase the value of a variable by 1 after the current value is used in an expression. To understand it, look at the below example.

Assume we have a variable $number that stores value 1.

$number = 1;

If we apply a pre-decrement operator to it.

$result = $number++;

If you print the $result variable, it will return the original value of the $number variable which is 1. Do you know why? It is because we use the post-increment operator ($number++), it first assigns the current value of $number the variable to $result. After that assignment is made, the $number is incremented by 1.

$number = 1;
$result = $number++;
echo $result;
// output: 1

But if we print the $number instead of the $result variable then their output would be different.

$number = 1;
$result = $number++;
echo $number;
// output: 2

In the above code, we printed the $number variable after applying the post-increment operator which increased its value by 1. So when we print the $number it returns the updated value which is 2.

Post-decrement ($number–):

The post-decrement operator is used to decrease the value of a variable by 1 after the current value is used in an expression. To understand it, look at the below example.

$number = 5;

If we apply a post-decrement operator,

$number = 5;
$result = $number--;
echo $result; // output: 5
echo $number; // output: 4

In this example of post-decrement operation ($number--), the current value of $number is used in the expression and then decreased by 1.

Conclusion:

This article explains the increment and decrement operators along with examples. Furthermore, it also provides a comprehensive explanation of the Pre-increment, Pre-decrement, Post-increment, and Post-decrement operators, complemented by examples and a helpful table for a concise reference.


Written by
I am Siraj, a results-driven web developer with three years of hands-on experience in both frontend and backend development. My passion for creating seamless digital experiences drives me to continuously innovate and excel in my field.

Share on:

Related Posts

Improved by: Ayaz Ali Shah

Tags: PHP,