Skip to main content

Operator Precedence

The precedence of Hack operators is shown in the table below.

Operators higher in the table have a higher precedence (binding more tightly). Binary operators on the same row are evaluated according to their associativity.

OperatorDescriptionAssociativity
\Namespace separatorLeft
::Scope resolutionLeft
[]Index resolution for Hack Arrays and StringsLeft
->, ?->Property selection and null-safe property selectionLeft
newObject creation & Memory allocationNone
#, ()Enum class labels and Function callingLeft
cloneObject cloning (shallowly, not deeply)None
readonly, await, ++ -- (postfix)Using readonly, Suspending an async function, and Incrementing / Decrementing (postfix)Right
(int) (float) (string), **, @, ++ -- (prefix)Casting, Exponentiation, Suppressing errors, and Incrementing / Decrementing (prefix)Right
is, as ?asType checks / Type assertionsLeft
!, ~, + - (one argument)Logical negation, Bitwise negation, and Unary Addition / SubtractionRight
* / %Multiplication, Division, and ModuloLeft
., + - (two arguments)String concatenation and Addition / SubtractionLeft
<< >>Bitwise shifting (left and right)Left
< <= > >=, <=>Comparison operators and Spaceship operatorNone
=== !== == !=Equality operatorsNone
&Bitwise ANDLeft
^Bitwise XORLeft
|Bitwise ORLeft
&&Logical ANDLeft
||Logical ORLeft
??Coalesce operatorRight
? :, ?:Ternary evaluation and Elvis operatorLeft
|>Pipe / Chain function callsLeft
= += -= .= *= /= %= <<= >>= &= ^= |=, ??=Assignment operators and Coalescing assignment operatorRight
echoWrite to standard outputRight
include requireInclude or Require a scriptLeft