PHP Operators: Operator Priority



PHP Operators Step By Step Tutorial - Part 15: For the complex expression which entangled of many operator and operand, the operators will be done by PHP according to its priority.

Example:

x = 24 - 20 / 4

For that, 24/4 will be done first, then the result of the division will become the reduction of 24 so that yields 19 non 1. That thing is caused by the priority of operator/higher than operator -.

The table of operator priority:

Priority Operators Workmanship sequence
1 new non-associative
2 [ right
3 !
~
++
-- (int) (float) (string) (array)
(object) @
right
4 *
/
%
left
5 +
-
.
left
6 <<
>>
left
7 <
<=
>
>=
non-associative
8 ==
!=
===
!==
non-associative
9 & left
10 ^ left
11 | left
12 && left
13 || left
14 ?: left
15 =
+=
-=
*=
/=
.=
%=
&=
|=^=
<<=
>>=
left
16 print right
17 and left
18 xor left
19 or left
20 , left

Operator in the same line have the same priority. Pay attention to the example of using string operator at script as following:

File name: operatorpriority.php

<html>
<head>
<title>Operator Priority</title>
</head>

<body>
<pre>
$a = 25;
$b = 10;
$c = 15;
$result = $a + $b * $c / 3;
</pre>
<h3>Processed by PHP</h3>
<?
$a = 25;
$b = 10;
$c = 15;
$result = $a + $b * $c / 3;
echo ("Result = $result");
?>
</body>
</html>
php operator priority


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, Operator Priority Category: PHP Basic Post : March 08th 2008 Read: 2,489 Bookmark and Share

blog comments powered by Disqus