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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access