PHP Increment and Decrement Operators: Modifying Variable Values

Rumman Ansari   Software Engineer   2024-07-09 10:09:28   5659  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

The PHP increment operators are used to increment a variable's value.

The PHP decrement operators are used to decrement a variable's value.

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

Example: ++$x Pre-increment

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 15;  
echo ++$x;
?>  

</body>
</html>


Output:

This will produce the following result



16


Example: $x++ Post-increment

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 15;  
echo $x++;
?>  

</body>
</html>



Output:

This will produce the following result



15


Example: --$x Pre-decrement

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 15;  
echo --$x;
?>  

</body>
</html>



Output:

This will produce the following result



14


Example: $x-- Post-decrement

Code:



<!DOCTYPE html>
<html>
<body>

<?php
$x = 15;  
echo $x--;
?>  

</body>
</html>



Output:

This will produce the following result



15