PHP Increment/Decrement Operators are used to increase or decrease the value of a variable by a certain amount.
They are essential in performing mathematical operations, loops, and other basic programming tasks.
The two types of Increment/Decrement Operators:
Pre-Increment/Decrement Operators
Post-Increment/Decrement Operators
Pre-Increment/Decrement Operators
Pre-Increment/Decrement Operators increase/decrease the value of a variable before the current expression is evaluated.
Post-Increment/Decrement Operators
Post-Increment/Decrement Operators increase/decrease the value of a variable after the current expression is evaluated.
Operator
Name
Syntax
Operation
++
Pre-Increment
++$a
First increments $a by one, then return $a
--
Pre-Decrement
–$a
First decrements $a by one, then return $a
++
Post-Increment
$a++
First returns $a, then increment it by one
--
Post-Decrement
$a–
First returns $a, then decrement it by one
Sample Code
<?php
$a = 2;
echo ++$a, " First increments then prints <br>";
echo $a, "<br>";
$a = 2;
echo $a++, " First prints then increments <br>";
echo $a, "<br>";
$a = 2;
echo --$a, " First decrements then prints <br>";
echo $a, "<br>";
$a = 2;
echo $a--, " First prints then decrements <br>";
echo $a;
?>
Output
3 First increments then prints
3
2 First prints then increments
3
1 First decrements then prints
1
2 First prints then decrements
1
Related Searches to PHP Increment/Decrement Operators - Increment and Decrement Operators in PHP
php increment and decrement operators program increment and decrement operators in php with examples php program using increment and decrement operators increment operator in php example increment operator example example of increment and decrement operator what is an increment operator php increment number by 1 logical operators in php php increment by 2 bitwise operators in php string operators in php arithmetic operators in php php operators