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);
Code language: PHP (php)
Output:
int(30)
Code language: PHP (php)
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);
Code language: PHP (php)
Output:
float(12)
Code language: PHP (php)
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);
Code language: PHP (php)
Output:
float(0.30000000000000004)
Code language: PHP (php)
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);
Code language: PHP (php)
Output:
bool(false)
Code language: PHP (php)
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;
Code language: PHP (php)
Output:
<?php
$x = 1 + '2';
echo $x;
Code language: PHP (php)
If the string contains a non-numeric value, PHP will issue a warning. For example:
<?php
$x = 1 + '2a';
echo $x;
Code language: PHP (php)
Warning:
Warning: A non-numeric value encountered
Code language: PHP (php)
If PHP cannot convert the string to a number, it’ll issue fatal and type errors:
<?php
$x = 1 + 'a';
echo $x;
Code language: PHP (php)
Error:
Fatal error: Uncaught TypeError: Unsupported operand types: int + string
TypeError: Unsupported operand types: int + string
Code language: PHP (php)
Subtraction operator #
The subtraction operator subtracts the second from the first:
<?php
$x = 20;
$y = 10;
$z = $x - $y;
var_dump($z);
Code language: PHP (php)
Output:
int(10)
Code language: PHP (php)
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);
Code language: PHP (php)
Output:
float(2)
Code language: PHP (php)
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);
Code language: PHP (php)
Output:
float(0.19999999999999998)
Code language: PHP (php)
Multiplication Operator #
The multiplication operator multiplies two numbers. For example:
<?php
$x = 10;
$y = 20;
$z = $x * $y;
var_dump($z);
Code language: PHP (php)
Output:
int(200)
Code language: PHP (php)
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);
Code language: PHP (php)
Output:
float(10)
Code language: PHP (php)
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);
Code language: PHP (php)
Output:
int(2)
Code language: PHP (php)
If you attempt to divide a number by zero, you’ll get a fatal error:
<?php
$x = 10 / 0;
Code language: PHP (php)
Error:
Fatal error: Uncaught DivisionByZeroError: Division by zero
Code language: PHP (php)
Modulus operator #
The modulus operator (%
) returns the remainder of the division:
<?php
$x = 5;
$y = 2;
$z = $x / $y;
var_dump($z);
Code language: PHP (php)
Output:
float(2.5)
Code language: PHP (php)
Exponentiation operator #
The exponentiation operator (**
) raises a number to the power of another number. For example:
<?php
$result = 10 ** 3;
var_dump($result);
Code language: PHP (php)
Output:
int(1000)
Code language: PHP (php)
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.