PHP Operator Precedence

PHPServer Side ProgrammingProgramming

Introduction

Precedence of operators decides the order of execution of operators in an expression. For example in 2+6/3, division of 6/3 is done first and then addition of 2+2 takesplace because division operator / has higher precedence over addition operator +. To force a certain operator to be called before other, parentheses should be used. In this example, (2+6)/3 performs addition first, followed by division.

Some operators may have same level of precedence. In that case, the order of associativity (either left or right) decides the order of operations. Operators of same precedence level but are non-associativem cannot be used next to each other. Following table lists PHP operators with decreasing order of precedence

Operatorspurpose
clone newclone and new
**exponentiation
++ --increment/decrement
~(int) (float) (string) (array) (object) (bool)casting
instanceoftypes
!logical
* /multiplication/division
%modulo
+ - .arithmetic and string
<< >>bitwise shift
< <= > >=comparison
== != === !== <> <=>comparison
&bitwise and/references
^bitwise XOR
|bitwise OR
&&logical and
||logical or
??null coalescing
? :ternary
= += -= *= **= /= .= %= &= |= ^= <<= >>= ??=assignment operators
yield fromyield from
yieldyield
printprint
andlogical
xorlogical
orlogical
raja
Published on 19-Sep-2020 13:53:05
Advertisements