PAWDD E-Training
17May/101

Increment and Decrement Operator

As you have probably thought prior to coming here, "I wish there was a way to increase or decrease a number without the need to use plus and minus operators over and over." Well if that was what your were thinking, then your idea has been realized with PHP "++"Increment and "--"Decrement.

This works the same in PHP and in JavaScript!

The way all this works is that normally to increase or decrease a number by one, you would need to do the following:

++/- - Pre-Increment / Decrement

<?php
 
$a = 10; 
//we need to increase $a by 1, normally we would do the following...
$a = $a + 1; // $a is now 11
 
//we need to decrease $a by 1, normally we would do the following...
$a = $a - 1; // $a is now 9
 
//There is a better way
$b = 10;
++$b //$b = 11 because we Incremented it by 1 first, by using the Increment operator "--" then returned its value
--$b //$b is 10 again because we first decremented it by 1 using the decrement operator "--" then returned its value
 
//The value of $b has literally changed, since the operator appeared prior to our variable
 
?>

With the above example showing how to increment and decrement a number by one, there is a little bit of a tricky situation happening behind the scenes. Since we placed the increment and decrement operator prior to the variable, in actuality behind the scenes PHP parser see the following :

PHP sees ++$a or --$a and literally, what takes place is that the PHP parser takes the value of $a and increments or decrements it by one, and then returns the value of $a incremented or decremented. The value of $b has literally changed, since the operator appeared prior to our variable, and that altered variable is then applied immediatly



However, in the following example something a little different takes place... Consider the following example where we move the increment/decrement operator after the variable we are working with.

Post-Increment / Decrement ++/- -

<?php
 
$a = 10; 
//we need to increase $a by 1, normally we would do the following...
$a = $a + 1; // $a is now 11
 
//we need to decrease $a by 1, normally we would do the following...
$a = $a - 1; // $a is now 9
 
//There is a different way with different results than the prior example
 
$b = 10;
echo $b++; //what will echo is 10, however if we echo it again it will show 11
echo $b //will be 11 because it was echoed after it was incremented
 
echo $b--; //what will echo is 10, however if we echo it again it will show 9
echo $b //will be 9 because it was echoed after it was decremented
 
?>

The returned value for $b remains as it was prior to the operator acting upon it because the variable $b appears prior to the operator. Since the operator is after the variable it then makes the value chang, and when we use it a second time, it has the altered value because the operator took place after the variable.

Therefore, to make it clear; placing the operator prior to the number makes PHP act upon the variable prior to returning it. Otherwise, if the operator is placed after the variable, PHP acts upon the variable after its value is returned, meaning it returns the initial value first, and is then acted upon. Therefore, the second usage of the variable is where the alteration is noticed when the operator is placed after the variable.

Dashboard

Training Categories

Shaded Pixel LLC

Monthly Archive

Page Links

PAWDD E-Training