2.6. Building Expressions
When computing the value of an expression made out of several operators, PHP evaluates operators according to their precedence value, as shown in Table 2.13. Operators with lower precedence values evaluate first. Consider the evaluation of 2 + 2 * 2. Since the multiplication operator * has precedence over the addition operator +, evaluation begins with the computation of 2 * 2. PHP then adds 2 to 4 and returns the result of 6.
Precedence alone, however, is not enough. Consider the expression 12 / 2 * 3. Both operators appearing in this expression, division and multiplication, have the same precedence. However, the result of this expression will vary depending on which operation we perform first. That is, (12 / 2) * 3 ...
Get Core PHP Programming, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.