|
| MATHEMATICAL OPERATORS IN PHP
PHP can do mathematical operations on numbers.
Example:
$total= 1 + 2;
echo $total; //outputs 3
PHP can also do mathematical operations on variables that contain numbers.
Example:
$firstNumber=5;
$total= $firstNumber + 2;
echo $total; //outputs 7
Example:
$firstNumber=5;
$secondNumber=18;
$total= $firstNumber + $secondNumber;
echo $total; //outputs 23
PHP can perform addition (as above), subtraction, division, multiplication and modulus.
SUBTRACTION:
Example:
$total= 10 - 1;
echo $total; //outputs 9
Example:
$firstNumber=5;
$total= $firstNumber - 2;
echo $total; //outputs 3
Example:
$firstNumber=5;
$secondNumber=18;
$total= $firstNumber - $secondNumber;
echo $total; //outputs -13
DIVISION:
Example:
$total= 10 / 2;
echo $total; //outputs 5
Example:
$firstNumber=6;
$total= $firstNumber / 2;
echo $total; //outputs 3
Example:
$firstNumber=20;
$secondNumber=10;
$total= $firstNumber / $secondNumber;
echo $total; //outputs 2
MULTIPLICATION:
Example:
$total= 7 * 2;
echo $total; //outputs 14
Example:
$firstNumber=6;
$total= $firstNumber * 100;
echo $total; //outputs 600
Example:
$firstNumber=20;
$secondNumber=10;
$total= $firstNumber * $secondNumber;
echo $total; //outputs 200
MODULUS:
Modulus is the remainder after a division. The operator % is used.
Example:
$total= 7 % 2;
echo $total; //outputs 1
1 is calculated as follows:
2 can go into 7 three times whole. Therefore 2 * 3 = 6. Then 7-6=1. Therefore
1 is the answer.
|
|