PHP Increment and Decrement Operators: Modifying Variable Values
☰Fullscreen
Table of Content:
The PHP increment operators are used to increment a variable's value.
The PHP decrement operators are used to decrement a variable's value.
Operator | Name | Description |
---|---|---|
++$x | Pre-increment | Increments $x by one, then returns $x |
$x++ | Post-increment | Returns $x, then increments $x by one |
--$x | Pre-decrement | Decrements $x by one, then returns $x |
$x-- | Post-decrement | Returns $x, then decrements $x by one |
Example: ++$x Pre-increment
Code:
Output:
This will produce the following result
16
Example: $x++ Post-increment
Code:
Output:
This will produce the following result
15
Example: --$x Pre-decrement
Code:
Output:
This will produce the following result
14
Example: $x-- Post-decrement
Code:
Output:
This will produce the following result
15