PHP Increment and Decrement Operators

हिन्दी | PHP Incrementing and Decrementing Operators in Hindi


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 –

  • Pre-increment & Pre-decrement
  • 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.


Incrementing and Decrementing Operators

Operator Name Description
++$x Pre-increment Increments $x by one, then returns $x.
$x++ Post-increment Returns $x, then increments $x by one.
–$x Pre-decrement Decrements $x by one, then returns $x.
$x– Post-decrement Returns $x, then Decrements $x by one.

Pre – Increment and 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 and 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

Official Resources

Leave a Reply

Your email address will not be published. Required fields are marked *