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 you with 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, it’ll convert them to numeric values before performing the arithmetic operation.
The following table illustrates the arithmetic operators in PHP:
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. |
PHP arithmetic operator examples
The following example uses the arithmetic operators:
<?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
Code language: HTML, XML (xml)
Did you find this tutorial useful?