Increment and Decrement Operations in PHP
Consider the following code:
<?php
$num = 0;
$num = $num + 1; // add the number 1 to the variable $num
echo $num; // will output 1
?>
As you already know, this code can be rewritten
in a shortened form using the += operator:
<?php
$num = 0;
$num += 1; // add the number 1 to the variable $num
echo $num; // will output 1
?>
In fact, adding one is so common in programming
that an even more abbreviated syntax was invented for this operation
- a special operator ++ (increment),
which increases the value of a variable by 1.
Let's rewrite our code using it:
<?php
$num = 0;
$num++; // add the number 1 to the variable a
echo $num; // will output 1
?>
In addition to the ++ operation, there is also the
-- (decrement) operation, which decreases
the value of a variable by 1. See the example:
<?php
$num = 0;
$num--; // subtract the number 1 from the variable $num
echo $num; // will output -1
?>
Modify this code so that it uses increment and decrement operations:
<?php
$num = 10;
$num = $num + 1;
$num = $num + 1;
$num = $num - 1;
echo $num;
?>