PHP Increment and Decrement Operator
Last updated onThe increment and decrement are operators that can be used to increase or decrease the variable values which have a string or an integer as a data type in PHP. That means the increment and decrement operators are not working with objects, arrays, Booleans, and other data types.
The used operators in PHP are the same in C style pre and post (increment and decrement).
Anyway, the following table shows you a list of the increment and decrement operators that are working in PHP.
The increment operators
Name | Operator Pattern | Result |
---|---|---|
Pre Increment | ++$z | To increase the $z variable by 1 then return $z |
Post Increment | $z++ | To return the $z and then increase it by 1 |
The decrement operators
Name | Operator Pattern | Result |
---|---|---|
Pre Decrement | –$z | To decrease the $z variable by 1 then return $z |
Post Decrement | $z– | To return the $z and then decrease it by 1 |
Anyway, there is another data type can work with only increment operator but it doesn’t effect of the exactly needed result. It is null (no value), when you use it with increment it will show you a result with 1.
Let’s take each one in-depth with examples.
Post Increment with Example
The pre-increment increments the variable by 1 and then return the variable which means if you have 50 assigned to the $c variable then you added a pre increment $c++, it would increase the $c by 1 so the final result should be 51.
The full example would be like the below.
<?php
$c = 50;
// the assigned value is 50
$c++; // Return the previous value (50)
// => Increase the $c by 1
echo $c; // => the output is 51
echo $c++; // Return the previous value (51)
// => Increase the $c by 1
echo $c; // the output is 52
?>
If you didn’t understand it check the following figure to learn more about it.
Let’s see what is happening with arithmetic operators.
<?php
$x = 10;
$y = 10;
$z = $x++ + $y++;
echo $z; // 20
?>
The output of this example would be 20 and not 22 because this $z variable only returns the previous value of the $x and $y.
Let’s see another example with subtraction.
<?php
$x = 100;
$y = 50;
$z = $x++ - $y++;
echo $z; // 50
?>
Pre Increment with examples
The pre increment to increase the variable by 1 and then returns the value so it is affecting in the current line. Check the following example.
<?php
$c = 100;
echo ++$c; // 101
?>
In the post increment it only return the previous value but here return the current value because it assigns the new value then return it in the same time.
See another example.
<?php
$b = 10;
++$b;
echo $b; // 11
?>
For arithmetic operators.
<?php
$x = 50;
$c = 100;
echo ++$x + ++$c; // 152
echo ++$x - ++$c; // -50
?>
Pre Decrement with examples
The pre decrement to decrease the value by 1 and then return the value. For example.
<?php
$x = 5;
echo --$x; // 4
?>
For another example.
<?php
$y = 20;
--$y;
echo $y; // 19
?>
Use the pre decrement with arithmetic operators.
<?php
$y = 55;
$x = 20;
echo --$y + --$x; // 73
echo --$y - --$x; // 35
?>
Post Decrement with Examples
The post decrement to return the variable value and then decrease the variable by 1 for example.
<?php
$x = 40;
echo $x--; // 40
echo $x; // 39
?>
Use the post decrement with arithmetic operators.
<?php
$x = 10;
// => 10 + 9
echo $x-- + $x--; // 19
echo $x-- - $x--; //
?>
Use the PHP String Data Type with Decrement and Increment
In this section the decrement and increment depending on the type juggling because it dynamically converting the string to numbers.
Let’s see an example.
<?php
$x = "this is number 5";
$x++;
echo $x;
?>
So the result would be like the following
this is number 6
For another example.
<?php
$z = "this number 150 will not increase";
echo ++$z; // 150
echo $z;
?>
To learn more about this case you have to read the PHP casting article.
Wrapping Up
In this article you learned more about the decrement and the increment. Also you understood how it does work in the arithmetic operators.
To learn more examples, you have to visit the PHP manual.
Thank you for reading, stay tuned for my next article.