Increment & Decrement in PHP

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(++)

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.

     $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(- -)

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.


    $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.