PHP increment and decrement operators

In PHP the ++ and the -- sign are increment and decrement operators. Increment means increase and decrement means decrease that means increment operator increase the variable value and decrement operator decrease the variable value.

The PHP increment operator increases it's variable value by one and the PHP decrement operator decreases it's variable value by one.

Suppose that we have a variable $x. We want to increase it's value one. Consider the statement below 
$x=10 but we want to increase it's value one so that the new form is 
$x=$x+1
We can rewrite this by using PHP increment operator
$x++;
Similarly in PHP decrement operator we can write $x--; 

Each PHP increment and decrement operators has two types: 
(++$x) Pre-increment : It first increment $x value one and then return the value of $X.

($x++) Post-increment :It first return $x value and then increment the value of $x.
(--$x) Pre-decrement : It first decrement $x value one and then return the value of $x.

($x--) Post-decrement : It return $x value and then decrement one.

Now we want to see an example that describe how actually works PHP increment and decrement operators.
<?php
$a=20;
echo ++$a;
echo "<br>";
echo $a++;
echo "<br>";
$b=30;
echo --$b;
echo "<br>";
echo $b--;
?>

Output: 
21
21
29
29



Comments

Popular posts from this blog

How to construct a B+ tree with example

How to show only month and year fields in android Date-picker?

Visitor Counter Script Using PHP