PHP Tutorial PHP Advanced PHP References

PHP - arithmetic operators example



The example below shows the usage of arithmetic operators - addition(+), subtraction(-), multiply(*), division(/), exponent(**) and modulo(%) operators.

<?php
$a = 11;
$b = 2;

echo "a = $a, b = $b\n\n";

//Add a and b
$result_add = $a + $b;
echo "a + b = $result_add\n";

//Subtract b from a
$result_sub = $a - $b;
echo "a - b = $result_sub\n";

//Multiply a and b
$result_mul = $a * $b;
echo "a * b = $result_mul\n";

//Divide a by b
$result_div = $a / $b;
echo "a / b = $result_div\n";

//a raised to the power of b
$result_pow = $a ** $b;
echo "a ** b = $result_pow\n";

//returns remainder of integer division
$result_modulo = $a % $b;
echo "a % b = $result_modulo\n";  
?>

The output of the above code will be:

a = 11, b = 2

a + b = 13
a - b = 9
a * b = 22
a / b = 5.5
a ** b = 121
a % b = 1

❮ PHP - Operators