What are the arithmetic operators in PHP?
Arithmetic operators are used to do maths on numbers. PHP has these arithmetic operators:
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| – | Subtraction | 10 – 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3.3333333333333 |
| % | Modulus (remainder) | 10 % 3 | 1 |
| ** | Exponentiation (power) | 10 ** 3 | 1000 |
<?php
$a = 10;
$b = 3;
echo ($a + $b) . "<br>";
echo ($a - $b) . "<br>";
echo ($a * $b) . "<br>";
echo ($a / $b) . "<br>";
echo ($a % $b) . "<br>";
echo ($a ** $b);
?>
Output:
Tutorials Class - Output Window
13
7
30
3.3333333333333
1
1000
Division always gives a float when the result is not a whole number. Use intdiv() if you need only the whole part. Read all operators in PHP Operators.
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Operators |
| Exercises & Assignments |
|---|
| Write a PHP program to add two numbers |
| Write a PHP program to check whether a number is positive, negative or zero |