Section 6.11. Operator Precedence and Associativity


6.11. Operator Precedence and Associativity

Like many languages, PHP has a set of rules (known as operator precedence and associativity ) that decide how complicated expressions are processed. For example:

     $foo = 5 * 10 - 1; 

Should $foo be 49 or 45? If you cannot see why there are two possibilities, break them up using parentheses like this:

     $foo = (5 * 10) - 1     $foo = 5 * (10 - 1);

In the first example, five is multiplied by ten, then one is subtracted from the result. But in the second example, ten has one subtracted from it, making nine, then that result is multiplied by five. If there is ambiguity in your expressions, PHP will resolve them according to its internal set of rules about operator precedence.

However, there's more to it than thatconsider the following statement:

     $foo = 5 - 5 - 5; 

Like the previous statement, this can have two possible results, 5 and -5. Here is how those two possibilities would look if we made our intentions explicit with parentheses:

     $foo = 5 - (5 - 5);     $foo = (5 - 5) - 5;

In this example, it is operator associativity that governs which answer is correct. PHP has been programmed to consider each operator left-associative, right-associative, or non-associative. For example, given the make-believe operator m, it might be right-associative and therefore treated like this:

     $foo = $a  $b  $c;     // would be treated as...     $foo = ($a  ($b  $c));

If PHP is programmed with m as left-associative, it would start working from the left:

     $foo = $a  $b  $c;     // would be treated as...     $foo = (($a  $b)  $c);

The equation 5 - 5 - 5 results in -5 because the subtraction operator is left-associative, giving (5 - 5) - 5.

These rules are only enforced if you fail to be explicit about your instructions. Unless you have very specific reason to do otherwise, you should always use parentheses in your expressions to make your actual meaning very clearboth to PHP and to others reading your code.

If you must rely on PHP's built-in rules for precedence and associativity, refer to Table 6-8 for the complete list of operators, precedence, and their associativity, ordered by the lowest-precedence operator to the highest-precedence operator:

Table 6-8. Operators, precedence, and their associativity

Operators

 

Associativity

,

Left

"$x, $y, $z" is "($x, $y), $z"

or

Left

"$x OR $y OR $z" is "($x OR $y) OR $z"

xor

left

"x XOR y XOR z" is "($x XOR $y) XOR $z"

and

Left

"x AND y AND z" is "(x AND y) AND z"

= += -= * = /= .= %= &= |= ^= <<= gt;>=

Right

"$x /= $y /= $z" is "$x /= ($y /= $z)"

? :

Left

 

||

Left

; "$x || $y || $z" is "($x || $y) || $z"

&&

Left

"$x && $y && $z" is "($x && $y) && $z"

|

Left

"$x | $y | $z" is "($x | $y) | $z"

^

Left

"$x ^ $y ^ $z" is "($x ^ $y) ^ $z"

&

Left

"$x & $y & $z" is "($x & $y) & $z"

= = != === !==

Non-associative

 

< < = > >=

Non-associative

 

<< >>

Left

"$x >> $y >> $z" is "($x >> $y) >> $z"

+ - .

Left

"$x - $y - $z" is "($x - $y) - $z"

* / %

Left

"$x / $y / $z" is "($x / $y) / $z"

! ~ ++ -- (int) (float) (string)(array) (object) @

Right;

 

[

Right

 

new

Non-associative

 




PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net