Complete PHP Programming/Operator precedence

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents

[edit] Precedence

Precedence determines the priority given to certain operators to be performed earlier in a statement. If an operator has higher precedence, it doesn't mean that it is of greater importance; the opposite can often be true.

[edit] Associativity

When multiple operators occur that have the same precedence (whether multiple instances of the same operator or just different operators with the same precedence), it becomes important to consider the associativity: whether right (to left), left (to right), or non-associative.

[edit] Examples where associativity is irrelevant

In certain cases (as in the example below), especially where the same operator is present, the associativity may make no difference to the result.

The following...

$a = 5*2*3*4; // Equals 120

...with its left associativity is equivalent to:

$a = (((5*2)*3)*4); // Equals 120

However, in this case, right associativity would have produced the same result:

$a = (5*(2*(3*4))); // Would also equal 120

[edit] Examples where associativity is relevant in PHP (but not mathematically)

In mathematics, it may be considered irrelevant in which direction a calculation is performed for operators with the same precedence.

For example, the following...

$a = 5+3-2+8; // Equals 14

...is equivalent to this (left associative) statement:

$a = (((5+3)-2)+8); // Equals 14

And, if this were considered according to human conventions in mathematical calculations, the following equivalent right associative expression would produce the same result:

$a = (5+(3+(-2+8))); // Would also equal 14

However, since we are dealing with a linear computer language which doesn't know to convert the "2" into a negative number and group it with the "8" before adding it to the "3" (and then the "5"), if PHP were to perform the following expression in a strict right associative manner, the following (mistaken) outcome would occur:

$a = (5+(3-(2+8))); // Would equal -2

Thus, the associativity is relevant and should be memorized (though it is generally good practice to make one's groupings explicit anyhow--both to avoid mistakes and to improve readability for others looking at the code).

Similar problems occur with multiplication and division. Although with human convention, all adjacent multiplication and division groups would have the multiplication performed at the numerator level and the division at the denominator level, the PHP interpreter does not know to do this, so it is bound to set the left(-to-right) convention when explicit groupings (via parentheses--which have highest precedence) have not been made:

$a = 5 * 4 / 2 * 3; // Equals 30

This is equivalent to the left associative:

$a = (((5 * 4) / 2) * 3); // Also equals 30

However, as with the addition/substraction example above, performing this by right associativity (in a strictly reverse linear fashion) does not produce the same (intended) result:

$a = (5 * (4 / (2 * 3))); // Equals 3.33333