PHP has also increment and decrement operators. The increment operator (++) adds 1 to the operand value and while (–) decrement operator subtracts 1. We can use these operators as the following form:
++$a; or $a++; --$a; or $a--;
From the above ++$a is equivalent to (a+1) and –$a is equivalent to (a-1).
We can use increment and decrement operators in the two types like:
$a = $a++; returns $a and then add 1 to $a
$a = ++$a; first add 1 to $a and then return the result
$b = $b–; returns $a and then subtract 1 from $a
$b = –$b; first subtract 1 from $a and then return the result
From the above code, ++$a and $a++ give the same result when they form statement independently but on the other hand they behave differently when they are used in expression on the right hand side of the statement. See the following example:
$a =5; $x = ++$a;
In this case both variables (value of a and x) would be 6 and now see the following statements:
$a =5; $x = $a++;
In this case value of x would be 5 and a would be 6, because prefix operators first add 1 and then return the result. On the other hand postfix operator return (means first assigns the value to the variables on the left hand) and then add 1 to operand.
There is another interesting thing that needs to consider that increment and decrement operators can be used in limited way with characters as well. For example- you can add one to the character A, the returned value is B. However you cannot subtract from (decrement) character value.