PHP for the World Wide Web (Visual QuickStart Guide)
I l @ ve RuBoard |
Inevitably, after discussing the different sorts of mathematical operators, one comes to the discussion of precedence. Precedence refers to the order in which a series of calculations will be executed. For example, what will be the value of the following variable? $Number = 10 4 / 2; Will $Number be worth 3 (10 minus 4 equals 6 divided by 2 equals 3) or 8 (4 divided by 2 equals 2 subtracted from 10 equals 8)? The answer here is 8, because division takes precedence over subtraction. Appendix C: PHP Resources shows the complete list of operator precedence for PHP (including those we haven't covered yet). However, instead of attempting to memorize a large table of peculiar characters , my recommendation would be to bypass the whole concept by using parentheses. Parentheses always take precedence over any other operator. Thus: $Number = (10 4) / 2; $Number = 10 (4 / 2); In the first example, $Number is now equal to 3, and in the second example, $Number is equal to 8. Using parentheses in your calculations will insure that you never obtain peculiar results from them because of precedence. You can rewrite your script, combining multiple lines into one while still maintaining accuracy by using parentheses. To use parentheses to establish precedence:
Tip Watch that you match your parentheses consistently as you create your formulas (every opening parentheses requires a closing parentheses).
|
I l @ ve RuBoard |