PHP Increment/Decrement Operators - Increment and Decrement Operators in PHP



PHP Increment/Decrement Operators

  • 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
php-increment-decrement-operator

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