Since you can include multiple operators in a statement to create complex expressions, PHP needs some way to determine the order in which the operators will be evaluated. This order is called operator precedence. For example, consider the following expression:

$x = 5 + 2 * 6 ;

The value of $x is either 42 or 17, depending on the order the operators are evaluated. PHP follows these rules:

So, based on these rules, the expression above evaluates to 17 since the 2 is multiplied by the 6 and then the 5 is added to the result. If your intent was to get 42, you need to tell PHP, which you do with parentheses:

$x = ( 5 + 2 ) * 6 ;

So, if you are ever in doubt about what order PHP will evaluate an expression, use parentheses to specify what you want.

Since PHP has lots more variables, there's more to precedence that this but we'll come back to it once we've covered more operators.

The list of PHP operators also shows their precedence.