Home >Backend Development >PHP Tutorial >Operator Precedence in PHP
The PHP Operator Precedence actually specifies how two expressions are tightly bound together and evaluate first. Operator Precedence is also decided when and how the operators are grouped using different parenthesis types. Operator Precedence can be higher precedence or lower precedence or equal precedence. They are many inbuilt mathematics operators in PHP programming language too which are having different types of operator precedence based on the type of operator. Operator precedence of the PHP Programming Language helps a lot in performing mathematical calculations so easily. If the operator precedence concept is not available in any coding language then the logic of the program will become messy.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
The Operator Precedence of the PHP Programming Language works based on the type of the operator. The operator can be a mathematical operator or any operator who is like special characters. For mathematical operators, operator precedence of the PHP language is like BOD-MAS (Brackets, Order, Division, Multiplication, Addition, and Subtraction). All computers and coding languages follow this mathematical operator precedence at all times for performing many kinds of calculations so easily.
Here we are going to see the operator precedence to the characters from the higher operator precedence to the lower operator precedence.
“[ ]” Operator is given the 1st precedence,
Then ++, — , ~, (), @ got the 2nd operator precedence,
Then “instanceof” got the 3rd precedence,
4th one is “!”,
5th one is “ * , / , % ”,
6th precedence given to +, – and . ,
>> and << got 7th precedence,
>, <, >=, <= got 8th precedence,
== , ===, !=, !==, <> got 9th precedence,
& got 10th,
^ got 11th,
| got 12th,
&& got 13,
|| got 14,
?: got 15th ,
=, *=, /=, %=, +=, -=, =, &=, ^=, |=, <<=, >>=, => got 16th,
and got 17th ,
xor got 18th ,
or got 19th
and “,” got 20th operator precedence.
Here are the following examples mention below
Here in the below operator precedence example, at first, the numerical elements which are inside of the braces will be calculated first based on the BODMAS principle of calculation. So for the first echo statement (40-4)/9 will be calculated and leaves the result as “4”. For the second echo statement, 4/9 is calculated first which leaves the answer as “0.44444444444”. Then “5*8” will be calculated and then subtracted with this result to the 4/9 result and leaves “39.5555555556”.
Code:
<?php echo "This is the mathematical calculation by giving higher precedence to the elements which are inside of the brackets:: <br>"; echo (((5*8)-4)/9); echo "<br>"; echo "Mathematical calculation done by using BOD-MAS Rule::<br>"; echo (5*8-4/9); ?>
Output:
In the below example, based on the BODMAS principle 3 variable values calculation is done. At first, $n11, $n12, $n13 variables are created by assigning some numerical values. Then the addition and the multiplication of these variable values are calculated in two different ways. One way of calculation is normally assigning the operators in between the variables. The second way of calculation is by mentioning the braces and operations between them. Values that are in between the braces will be calculated first. In the first $ans1 variable, multiplication of n12 and n13 variable is done and then added the value of n11’s value. In the second $ans1 n11 and n22 variable value is calculated first and then multiplication is done with the n13 variable value.
Code:
<?php echo "Program to know how the mathematical operator precedence works :: <br>"; $n11 = 10; $n12 = 5; $n13 = 2; $ans1 = $n11 + $n12 * $n13; echo "The result of "; echo "$n11 + $n12 * $n13 = $ans1<br />"; $ans1 = ($n11 + $n12) * $n13; echo "The result of "; echo "($n11 + $n12) * $n13 = $ans1<br />"; ?>
Output:
In the below examples output, the value of x++, ++x, – – y values are shown to know what are the values of calculation. So the result will be calculated using “4+6/4*3”. Based on the BODMAS principle, 6/4 is calculated first and leaves 1.5 as the answer. Then 1.5*3 is calculated and leaves 4.5 and then 4+4.5 is calculated and leaves the result as 8.5. This illustration will let you know how the BODMAS principle is used.
Code:
<?php $x = 4; $y = 5; $a = 4; $b = 5; echo "First time x++ value :: "; echo $a++; echo "<br>"; echo "Second time ++x value :: "; echo ++$a; echo "<br>"; echo "First time - - y value :: "; echo --$b; echo "<br>"; echo "Second time - - y value :: "; echo --$b; echo "<br>"; $result = $x++ + ++$x / --$y * --$y; echo "The result of (x++)+(++x)/(- - y)*(- - y) :: "; echo "{$result} <br/>"; ?>
Output:
In the below example, to know the operator precedence, different types of operator symbols are used for the result variable. The var_dump(result) will be true only if both the elements in the braces are TRUE. You can know what are the values of – – a, a – -, ++b, – – c are shown in the output for better understanding. Now the values of those are used to calculate whether the conditions of the result variable satisfies or not. If satisfied and both the braces conditions are TRUE then the var_dump() function will leave the result as TRUE.
Code:
<?php $a = 2; $b = 8; $c = 8; $d = 2; $e = 8; $f = 8; echo "Actual and Original 'a' variable value :: $a <br>"; echo "Actual and Original 'b' variable value :: $b <br>"; echo "Actual and Original 'c' variable value :: $c <br>"; echo "The value of - - a ::". --$d." <br>"; echo "The value of a - - ::". $d--." <br>"; echo "The value of ++ b ::". ++$e." <br>"; echo "The value of - - a ::". --$f." <br>"; $result = ($a * $a <= $b * $a) && (--$a * $a-- !== ++$b - --$c); echo "After the completion of the above result statement 'a' value = {$a} <br/>"; echo "After the completion the above result statement 'b' value = {$b} <br/>"; echo "After the completion the above result statement 'c' value = {$c} <br/>"; var_dump($result); ?>
Output:
The above is the detailed content of Operator Precedence in PHP. For more information, please follow other related articles on the PHP Chinese website!