PHP BasicIntroduction to PHP PHP Starting Requirements Downloading XAMPP Local Server PHP Variable Concept and Data Type PHP Coding Syntax Showing Output in PHP PHP String Concatenation Increment & Decrement in PHP PHP Logical Operator and Usage Part-1 PHP Logical Operator and Usage Part-2 PHP Comparison Operators Arithmetic and Assignment Operators PHP Switch Case Statement For loop in PHP Super Global Variable $_REQUEST and $_GET Part-1 String Length in PHP Function Declaration and Usage
In programming language increment and decrement operator are used to increase or decrease current value of a variable. This is mainly used in the loop or some other places also. If the value of a variable needs to be changed then these two operators play an important role. Each increment and decrement have two different parts such as pre and post. Every time post-increment is used. But several times pre-increment is used inside the loop.
Increment operator is used to increase the value of a variable by 1. If the value of a variable is 5 then after using increment it will be 6. Hope that it will be clear after noticing the below code.
<?php $x = 5; echo $x . "
"; //value of x is 5 $x++; echo $x; // value of x is 6 ?>
After looking the code we find that the primary value of variable $x is 5. After using increment(++) operator the value of variable $x has become 6 from 5.
Decrement operator is used for decreasing the current value of a variable by 1. If we think the current value of a variable $p is 10. After using decrement operator it will be 9. It means that decrement operator does subtraction operation. Below example will be helpful for better clearance.
<?php $p = 10; echo $p . "
"; //value of x is 10 $p--; echo $p; // value of x is 9 ?>
Hope that you have got a better idea about increment and decrement operator. For better understanding practice with several examples.