PHP MYSQL - Operators - 5.4 Increment / Dercement Operators
PHP Increment / Decrement Operators
The PHP increment operators are used to increment a variable's value by adding one.
The PHP decrement operators are used to decrement a variable's value by subtracting one.
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 |
Prefix Increment Operator
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo ++$x;
?>
</body>
</html>
Output:
11
Example:
Postfix Increment Operator
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo $x++;
?>
</body>
</html>
Output:
10
Similarly prefix --, postfix -- operators works