Summary: in this tutorial, you will learn about the arithmetic operators including addition, subtraction, multiplication, division, exponentiation, and modulo to perform arithmetic operations.
Introduction to PHP arithmetic operators #
PHP provides common arithmetic operators that allow you to perform addition, subtraction, multiplication, division, exponentiation, and modulus operations.
The arithmetic operators require numeric values. If you apply an arithmetic operator to non-numeric values, PHP will attempt to convert them to numeric values before performing arithmetic operations.
The following table displays the arithmetic operators in PHP:
| Operator | Name | Example | Description |
|---|---|---|---|
| + | Addition | $x * $y | Return the sum of $x and $y |
| – | Subtraction | $x – $y | Return the difference of $x and $y |
| * | Multiplication | $x * $y | Return the product of $x and $y |
| / | Division | $x / $y | Return the quotient of $x and $y |
| % | Modulo | $x % $y | Return the remainder of $x divided by $y |
| ** | Exponentiation | $x ** $y | Return the result of raising $x to the power of $y. |
Addition operator #
The addition operator (+) adds two numbers and returns their sum. You can use the addition operator with integers and floats.
The sum of two integers will result in an integer:
<?php
$x = 10;
$y = 20;
$z = $x + $y;
var_dump($z);Output:
int(30)The sum of an integer with a float or two floats will result in a float:
<?php
$x = 10;
$y = 2.0;
$z = $x + $y;
var_dump($z);Output:
float(12)Computers cannot represent floats precisely. In some cases, the result will not be what you expect. For example:
<?php
$z = 0.1 + 0.1 + 0.1;
var_dump($z);Output:
float(0.30000000000000004)You’ll encounter an issue If you add floats and compare the result with another float. For example, the following returns false instead of true:
<?php
$z = 0.1 + 0.1 + 0.1;
var_dump($z === 0.3);Output:
bool(false)If you add a number and a string, PHP will attempt to convert the string to a number before performing the calculation:
<?php
$x = 1 + '2';
echo $x;Output:
<?php
$x = 1 + '2';
echo $x;If the string contains a non-numeric value, PHP will issue a warning. For example:
<?php
$x = 1 + '2a';
echo $x;Warning:
Warning: A non-numeric value encounteredIf PHP cannot convert the string to a number, it’ll issue fatal and type errors:
<?php
$x = 1 + 'a';
echo $x;Error:
Fatal error: Uncaught TypeError: Unsupported operand types: int + string
TypeError: Unsupported operand types: int + stringSubtraction operator #
The subtraction operator subtracts the second from the first:
<?php
$x = 20;
$y = 10;
$z = $x - $y;
var_dump($z);Output:
int(10)If you subtract a float from another float or integer, the result will be a float:
<?php
$x = 3.0;
$y = 1.0;
$z = $x - $y;
var_dump($z);Output:
float(2)Like the addition operator, subtracting a float from another will sometimes result in inaccurate numbers. For example:
<?php
$x = 0.3 - 0.1;
var_dump($x);Output:
float(0.19999999999999998)Multiplication Operator #
The multiplication operator multiplies two numbers. For example:
<?php
$x = 10;
$y = 20;
$z = $x * $y;
var_dump($z);Output:
int(200)When you multiply a float with an integer or float, the result will be a float:
<?php
$x = 10;
$y = 1.0;
$z = $x * $y;
var_dump($z);Output:
float(10)Division operator #
The division operator divides the first number by the second one. For example:
<?php
$x = 20;
$y = 10;
$z = $x / $y;
var_dump($z);Output:
int(2)If you attempt to divide a number by zero, you’ll get a fatal error:
<?php
$x = 10 / 0;Error:
Fatal error: Uncaught DivisionByZeroError: Division by zeroModulus operator #
The modulus operator (%) returns the remainder of the division:
<?php
$x = 5;
$y = 2;
$z = $x / $y;
var_dump($z);Output:
float(2.5)Exponentiation operator #
The exponentiation operator (**) raises a number to the power of another number. For example:
<?php
$result = 10 ** 3;
var_dump($result);Output:
int(1000)Summary #
- Use the addition operator (
+) to add two numbers. - Use the subtraction operator (
-) to subtract a second number from the first number. - Use the multiplication operator (
*) to multiply two numbers. - Use the division operator (
/) to divide the first number by the second. - Use the modulus operator (
%) to return the remainder of the division. - Use the exponentiation operator (
**)to raise a number to a power of another number.
Thank you for your feedback!