• Tutorial List

PHP Incrementing and Decrementing Operators

Last updated May 15, 2020
PHP Incrementing and Decrementing Operators

PHP Incrementing and Decrementing Operators

The PHP Increment Decrement operators are used to increment and decrement a variable’s value.

In PHP there are two types of incrementing and decrementing operators –

  1. Pre-increment & Pre-decrement
  2. Post-increment & Post-decrement

What is the difference between Pre and Post?

Pre first performs the operation then returns the value. But, the Post first returns the value and then performs the operation.


Table of the Incrementing and Decrementing Operators

OperatorNameDescription
++$xPre-incrementIncrements $x by one, then returns $x.
-$xPre-decrementDecrements $x by one, then returns $x.
$x++Post-incrementReturns $x, then increments $x by one.
$x–Post-decrementReturns $x, then Decrements $x by one.
PHP increment and decrement operators

Pre – Increment & Decrement Example

<?php
  $x = 7;
  $y = 4;

  echo "Pre-increment - ". ++$x."<br>";
  echo "Pre-decrement - ". --$y;
?>
Browser Output
Pre-increment – 8
Pre-decrement – 3

Post – Increment & Decrement Example

<?php
  $x = 7;
  $y = 4;

  echo "Post-increment - ". $x++ ."<br>";
  echo "Increment Value - ". $x ."<br>";

  echo "<hr/>";

  echo "Post-decrement - ". $y-- ."<br>";
  echo "Decrement Value - ". $y;
?>
Browser Output
Post-increment – 7
Increment Value – 8
Post-decrement – 4
Decrement Value – 3