PHP Operator Precedence

In this chapter you will learn:

  1. What is Operator Precedence
  2. A list of the operators in order of precedence (highest first):
  3. Use parentheses to overwrite Operator Precedence

Description

An operator with a higher precedence is executed before an operator with lower precedence. For example,

3 + 4 * 5

In the case of the example, * has a higher precedence than + , so PHP multiplies 4 by 5 first, then adds 3 to the result to get 23.

Precedence of Some PHP Operators (Highest First)

A list of the operators in order of precedence (highest first):


++  --  (increment/decrement)    /*from ja  va  2s . com*/
(int) (float) (string) (array) (object) (bool) (casting)    
! (not)    
* / % (arithmetic)    
+  -  . (arithmetic)    
<     < =  >     > =  <  >  (comparison)    
== != === !== (comparison)    
&&  (and)    
|| (or)    
= +=  - = *= /= .= %= (assignment)    
and     
xor    
or    

Parentheses

You can affect the order of execution of operators in an expression by using parentheses. So, for example, the following expression evaluates to 35:

( 3 + 4 ) * 5

Next chapter...

What you will learn in the next chapter:

  1. What is Code Blocks
  2. Syntax to create PHP code blocks
  3. Example - Use code block with if statement
Home » PHP Tutorial » PHP Operators
PHP Assignment Operators
PHP Arithmetic Operators
PHP Incrementing and Decrementing Operators
PHP Comparison Operators
PHP Logical Operators
PHP String Operators
PHP Ternary Operator
PHP Bitwise Operators
PHP Execution Operator
PHP Operator Precedence