13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Underst<strong>and</strong>ing Precedence <strong>and</strong> Associativity<br />

43<br />

Table 1.7 Operator Precedence in <strong>PHP</strong><br />

Associativity Operators<br />

left ,<br />

left<br />

or<br />

left<br />

xor<br />

left<br />

<strong>and</strong><br />

right<br />

print<br />

left = += -= *= /= .= %= &= |= ^= ~= =<br />

left ? :<br />

left ||<br />

left<br />

&&<br />

left |<br />

left<br />

^<br />

left<br />

&<br />

n/a == != === !==<br />

n/a < >=<br />

left ><br />

left + - .<br />

left * / %<br />

right<br />

! ~ ++ -- (int) (double) (string) (array) (object) @<br />

right []<br />

n/a<br />

new<br />

n/a ()<br />

Notice that we haven’t yet covered the operator with the highest precedence: plain old<br />

parentheses.The effect of using parentheses is to raise the precedence of whatever is contained<br />

within them.This is how you can deliberately manipulate or work around the<br />

precedence rules when you need to.<br />

Remember this part of the preceding example:<br />

$totalamount = $totalamount * (1 + $taxrate);<br />

If you had written<br />

$totalamount = $totalamount * 1 + $taxrate;<br />

the multiplication operation, having higher precedence than the addition operation,<br />

would be performed first, giving an incorrect result. By using the parentheses, you can<br />

force the subexpression 1 + $taxrate to be evaluated first.<br />

You can use as many sets of parentheses as you like in an expression.The innermost<br />

set of parentheses is evaluated first.<br />

Also note one other operator in this table we have not yet covered: the print language<br />

construct, which is equivalent to echo. Both constructs generate output.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!