Prev | Current Page 163 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

= %=&=
|= ^= <<= >>=
Right Assignment operators
AND XOR OR Left Boolean AND, Boolean XOR, Boolean OR
, Left Expression separation; example: $days =
array(1=>"Monday", 2=>"Tuesday")
CHAPTER 3 ?–  PHP BASICS 89
Operator Precedence
Operator precedence is a characteristic of operators that determines the order in
which they evaluate the operands surrounding them. PHP follows the standard
precedence rules used in elementary school math class. Consider a few examples:
$total_cost = $cost + $cost * 0.06;
This is the same as writing
$total_cost = $cost + ($cost * 0.06);
because the multiplication operator has higher precedence than the addition operator.
Operator Associativity
The associativity characteristic of an operator specifies how operations of the same
precedence (i.e., having the same precedence value, as displayed in Table 3-3) are
evaluated as they are executed. Associativity can be performed in two directions, left to
right or right to left. Left-to-right associativity means that the various operations making
up the expression are evaluated from left to right. Consider the following example:
$value = 3 * 4 * 5 * 7 * 2;
The preceding example is the same as the following:
$value = ((((3 * 4) * 5) * 7) * 2);
This expression results in the value 840 because the multiplication (*) operator is
left-to-right associative.
In contrast, right-to-left associativity evaluates operators of the same precedence
from right to left:
$c = 5;
print $value = $a = $b = $c;
The preceding example is the same as the following:
$c = 5;
$value = ($a = ($b = $c));
When this expression is evaluated, variables $value, $a, $b, and $c will all contain
the value 5 because the assignment operator (=) has right-to-left associativity.


Pages:
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175