Operators

Download php eBook

Introduction

An operator is something that takes one or more values (or expressions, in programming jargon) and yields another value (so that the construction itself becomes an expression).

Operators can be grouped according to the number of values they take.

Remarks

Operators 'operate' or act on one (unary operators such as !$a and ++$a), two (binary operators such as $a + $b or $a >> $b) or three (the only ternary operator is $a ? $b : $c) expressions.

Operator precedence influences how operators are grouped (as if there were parentheses). The following is a list of operators in order of there precendence (operators in the second column). If multiple operators are in one row, the grouping is determined by the code order, where the first column indicates the associativity (see examples).

AssociationOperator
left-> ::
noneclone new
left[
right**
right++ -- ~ (int) (float) (string) (array) (object) (bool) @
noneinstanceof
right!
left* / %
left+ - .
left<< >>
none< <= > >=
none== != === !== <> <=>
left&
left^
left|
left&&
left||
right??
left? :
right= += -= *= **= /= .= %= &= `
leftand
leftxor
leftor

Full information is at Stack Overflow.

Note that functions and language constructs (e.g. print) are always evaluated first, but any return value will be used according to the above precedence/associativity rules. Special care is needed if the parentheses after a language construct are omitted. E.g. echo 2 . print 3 + 4; echo's 721: the print part evaluates 3 + 4, prints the outcome 7 and returns 1. After that, 2 is echoed, concatenated with the return value of print (1).

Related Examples

Stats

848 Contributors: 37
Wednesday, July 19, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook