Arithmetic Operators in PHP : Tutorial



Arithmetic operators are the nothing but simple addition, subtraction, multipication and division operators that we use in our day to day life for basic calculations

Arithmetic Operators

- Subtraction
+ Addition
* Multiplication
/ Division
% Modulus

Division operator (/) returns a float value unless the two operands are integers.
Operands of Modulus (%) are converted to integers before they are processed.
The result of Modulus Operator (%) has the same sign as the dividend.
Example : Addition (+)
<?php

$a = 2;
$b = 2;

$c = -12;
$b = 22;

echo $a + $b   ."<br>";
echo $c + $d   ."<br>";
}

?>
Output

4 10


Example : Subtraction (-)
<?php

$a = 2;
$b = 2;

$c = -8;
$d = 2;

echo $a - $b   ."<br>";
echo $c - $d   ."<br>";
}

?>
Output

0 -10


Example : Division (/)
<?php

$a = 2;
$b = 2;

$c = -4;
$d = 5;

echo $a / $b   ."<br>";
echo $c / $d   ."<br>";
}

?>
Output

1 -0.8


Example : Multiplication (*)
<?php

$a = 2;
$b = 2;

$c = -2;
$d = 2;

echo $a * $b   ."<br>";
echo $c * $d   ."<br>";
}

?>
Output

4 -4


Example : Modulus (%)
<?php

$a = 2;
$b = 4;

$c = 4;
$d = 2;

$e = -3;
$f = 2;

$g = -3;
$h = -2;

echo $a % $b  ."<br>";
echo $c % $d  ."<br>";
echo $e % $f  ."<br>";
echo $g % $h  ."<br>";
}

?>
Output

2 0 -1 -1