phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP Operators: Arithmetic Operators

Tag: operator, Arithmetic Operators   Category: PHP Basic
post: 08 Mar 2008 read: 526


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

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting