Curriculum
In this tutorial, you will learn how to perform arithmetic operations using arithmetic operators such as addition, subtraction, multiplication, division, exponentiation, and modulo.
PHP includes common arithmetic operators for addition, subtraction, multiplication, division, exponentiation, and modulus operations.
Numeric values are required by the arithmetic operators. When you apply an arithmetic operator to a non-numeric value, it converts it to a numeric value first before performing the arithmetic operation.
The arithmetic operators in PHP are illustrated in the table below:
Operator | Name | Example | Description |
---|---|---|---|
+ | Addition | $x * $y | Return the sum of $x and $y |
– | Substration | $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 $y‘th power. |
The arithmetic operators are used in the following example:
<?php $x = 20; $y = 10; // add, subtract, and multiplication operators demo echo $x + $y; // 30 echo $x - $y; // 10 echo $x * $y; // 200 // division operator demo $z = $x / $y; // modulo demo $y = 15; echo $x % $y; // 5