PHP Operators Step By Step Tutorial - Part 2: Arithmetic Operators is operator that is used to conduct mathematical calculation.
Arithmetic Operators Table
| Operator |
Info |
Example |
| + |
Addition |
$p = 30 + 40 //$p = 70 |
| - |
Reduction |
$q = 4 - 1 //$q = 3 |
| * |
Multiplication |
$r = 5 * 5 //$r = 25 |
| / |
Division |
$s = 36/6 //$s = 6 |
| % |
Rest of to (modulo) |
$t = 9%8 //$t = 1 |
In the complex expression which is entangled of many operand, operator *, /, and % have the same priority but it is higher rather than operator + and -. For further information, just pay attention to the example as following:
$a = 20 + 4 % 4
4 % 4 will be done first, so that result is 21 not 6.
Pay attention to the example of using aritmatika operator at script as following :
File name : aritmatikaoperator.php
<html>
<head>
<title>Using Arithmetic Operators</title>
</head>
<body>
<?
print("Addition: <br>");
printf("9 + 7 = %d <br>", 9 + 7);
printf("9.5 + 7 = %f <br>", 9.5 + 7);
printf("9.5 + 7.5 = %f <br>", 9.5 + 7.5);
printf("-9.5 + 7.5 = %f <br>", -9.5 + 7.5);
print("Reduction: <br>");
printf("9 - 7 = %d <br>", 9 - 7);
printf("9.5 - 7 = %f <br>", 9.5 - 7);
printf("9.5 - 7.5 = %f <br>", 9.5 - 7.5);
printf("-9.5 - 7.5 = %f <br>", -9.5 - 7.5);
print("Multiplication: <br>");
printf("9 * 7 = %d <br>", 9 * 7);
printf("9.5 * 7 = %f <br>", 9.5 * 7);
printf("9.5 * 7.5 = %f <br>", 9.5 * 7.5);
printf("-9.5 * 7.5 = %f <br>", -9.5 * 7.5);
print("Division: <br>");
printf("9 / 7 = %d <br>", 9 / 7);
printf("9.5 / 7 = %f <br>", 9.5 / 7);
printf("9.5 / 7.5 = %f <br>", 9.5 / 7.5);
printf("-9.5 / 7.5 = %f <br>", -9.5 / 7.5);
print("Modulus: <br>");
print(" 9 % 7 = "); print(9 % 7 ."<br>");
print("9.5 % 7 = "); print(9.5 % 7 ."<br>");
print("9.5 % 7.5 = "); print( 9.5 % 7.5."<br>");
print("-9.5 % 7.5 = "); print( -9.5 % 7.5."<br>");
print("9.5 % -7.5 = "); print( 9.5 % -7.5."<br>");
print"<br>";
?>
</body>
</html>