Home >>PHP Tutorial >PHP Arithmetic Operators
PHP supports all standard arithmetic operations, as illustrated by the list of Operators .
Operatos | Description |
---|---|
+ | Add |
- | Subtract |
* | Multiply |
/ | Divide and return quotient |
% | Divide and return modulus |
PHP Arithmetic operators are used to perform mathematical operation on more than one operands.
Some of the standard arithmetic operators are +,-,*,/,%.
The use of some common arithmetic operators is here illustrated by an example as follows:-
<?php $x=10; $y=5; //addition $sum=$x+$y; echo "sum=".$sum."<br/>"; ?>
<?php $x=10; $y=5; //subtraction $sub=$x-$y; echo "sub=".$sub."<br/>"; ?>
<?php $x=10; $y=5; //Multiply $multiply=$x*$y; echo "Multiplication = ".$multiply."<br/>"; ?>
<?php $x=10; $y=5; //quotient $div=$x/$y; echo "Div = ".$div."<br/>"; ?>
<?php $x=10; $y=3; //remainder $rem=$x%$y; echo "remainder=".$rem."<br/>"; ?>
$x and $y are two integer variables here there are five blocks/modules in this example they are to preform addition, subtraction, multiplication, division and modulus respectively.
$x store the value 10, $y store the value 5. The output of first module is addition of two values 10 and 5 that is 15 ($x+$y=15).
The output of second module is subtraction of two values 10 and 5 that is 5 ($x-$y=5).
The output of third module is multiplication of two values 10 and 5 that is 50 ($x*$y=50).
The output of fourth module is division of two values 10 and 5 that is 2 ($x/$y=2).
The output of last module is modulus of two values 10 and 3 that is 1 ($x%$y=1).