Arithmetic operators are simple. What we learned in elementary school, you can call them mathematical operators. With these operators, you can add, subtract, divide and multiply the values you want.
Here are the arithmetic operators, names and examples in PHP:
Operator | Name | Example |
---|---|---|
+ | Addition | $a + $b |
– | Subtraction | $a - $b |
* | Multiplication | $a * $b |
/ | Division | $a / $b |
% | Module | $a % $b |
Let’s show them an example:
<?php
$a = 10;
$b = 20;
$c = 5;
$result = $a + $b - $c;
echo $result;
?>
It will display 25.
The value of $a
is 10, the value of $b
is 20, and the value of $c
is 5. We call $result
variable $a
plus $b
and minus $c
, which makes $result
10 + 20 - 5 = 25 that’s the value.
Let us show you a useful example:
<?php
$year = 2020;
$birth_year = 1999;
$age = $year - $birth_year;
echo $age;
?>
It will display 21. We subtracted our year of birth to 2020 and found our age.