PHP Operators Arithmetic Operators


Arithmetic Operators

With PHP's familiar arithmetic operators, adding, subtraction, multiplication, division, exponentiation, and modulus are all possible.

The arithmetic operators require numeric values. When you employ an arithmetic operator on a non-numeric value, it first transforms it to a numeric value before doing the calculation.

The arithmetic operators in PHP are shown in the table below:

Operator Name Example Description
+ Addition $x * $y Get the total of $x and $y.
- Subtraction $x – $y Calculate the difference between $x and $y.
* Multiplication $x * $y Return the $x and $y product.
/ Division $x / $y Return the $x and $y quotient.
% Modulo $x % $y Return $x divided by $y as a remainder.
** Exponentiation $x ** $y Raise $x to the $y'th power and return the result.

PHP arithmetic operator examples

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 ?>