The following table shows the order of precedence of the operators that we've studied until now:
Operator ** | Type Arithmetic |
---|---|
++, -- | Increasing/decreasing |
! | Logical |
*, /, % | Arithmetic |
+, - | Arithmetic |
<, <=, >, >= | Comparison |
==, !=, ===, !== | Comparison |
&& | Logical |
|| | Logical |
=, +=, -=, *=, /=, %=, **= | Assignment |
The expression 3+2*3 will first evaluate the product 2*3 and then the sum, so the result is 9 rather than 15.
To perform operations in a specific order, different from the natural order of precedence, force it by enclosing the operation within parentheses.
Hence, (3+2)*3 will first perform the sum and then the product, giving the result 15 this time.
<?php $a = 1; /*from w w w. j a v a 2 s . c o m*/ $b = 3; $c = true; $d = false; $e = $a + $b > 5 || $c; // true var_dump($e); $f = $e == true && !$d; // true var_dump($f); $g = ($a + $b) * 2 + 3 * 4; // 20 var_dump($g); ?>