PHP Arithmetic Operators

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:

+Addition$x * $yReturn the sum of $x and $y
Substration$x – $yReturn the difference of $x and $y
*Multiplication$x * $yReturn the product of $x and $y
/Division$x / $yReturn the quotient of $x and $y
%Modulo$x % $yReturn the remainder of $x divided by $y
**Exponentiation$x ** $yReturn 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?