PHP Operators: Arithmetic Operators



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>


Series this article:
PHP Operators: Introduction
PHP Operators: Arithmetic Operators
PHP Operators: Assignment Operators
PHP Operators: Assignment Operators Table
PHP Operators: Bitwise Operators
PHP Operators: Relation Operators
PHP Operators: Ternary Operator
PHP Operators: Error Control Operator
PHP Operators: Execution Operator
PHP Operators: Increment or Decrement Operator
php Operators: Logic Operator
PHP Operators: String Operator
PHP Operators: Array Operator
PHP Operators: Object Operator
PHP Operators: Operator Priority
PHP Constants: Introduction


Tag: operator, Arithmetic Operators Category: PHP Basic Post : March 08th 2008 Read: 1,705 Bookmark and Share

blog comments powered by Disqus