PHP Programing language

Incrementing And Decrementing Examples
Previous Home Next

In PHP is to increment (add 1 to) or decrement (Subtract 1 from ) values. Mainly there are more type to write a syntax for incrementing and decrementing like as:

//If you using shortcut assignment operator += like as:
$a += 1;
//And another way using for incrementing values: 
 the ++ operator, which you use like this:
$a =1;

Similarly this process is same way to work for decrement.

Note:If you using the ++ or -- operator after a variable, then the value in the variable is incremented after the rest of the statement is executed. If you put ++ or -- in front of a variable, the value in that variable is incremented or decremented before the rest of the statement is executed.

++$a Pre-Increment Increments $a by 1, then returns $a.
$a++ Post-Increment Returns $a, then increments $a by 1.
--$a Pre-Decrement Decrements $a by 1, then returns $a.
$a-- Post-Decrement Returns $a, then decrements $a by 1.


Example for Increment and Decrement

<title>Increment or decrement</title>
$a = 10;
echo 'Value of $a is :'.$a;
echo '<br />After Pre-increment value of $a ( i.e. ++$a ) is: '.++$a;
$a = 20;
echo '<br />Value of $a is :'.$a;
echo '<br />After Post-increment value of $a ( i.e. $a++ ) is: '.$a++;
$a = 30;
echo '<br />Value of $a is :'.$a;
echo '<br />After Pre-decrement value of $a ( i.e. --$a ) is: '.--$a;
$a = 40;
echo '<br />Value of $a is :'.$a;
echo '<br />After Post-decrement value of $a ( i.e. $a-- ) is: '.$a--;
Previous Home Next