PHP Operators

Operators are used to manipulate variables and expressions. A unary operator operates on a single variable or expression (the operand). For example, the ++ (increment) operator increases the value of its operand by one. A binary operator (the most commonly used kind of operator) operates on two operands. The + (addition) operator, for example, sums the value of its two operands. The ternary operator (?:) is a conditional operator used to select between two operands, depending on the value of a third. The main operators used in PHP are described below.



Arithmetic Operators (binary)
SymbolDescriptionUsageResult
+Addition$n + $mSum of $n and $m
-Subtraction$n - $mDifference of $n and $m
*Multiplication$n * $mProduct of $n and $m
/Division$n / $mQuotient of $n and $m (note that a floating-point value
is returned unless both operands are integers, and the
first operand is divisible by the second)
%Modulus$n % $mRemainder of $n divided by $m (converts floating-point
operands to integers by removing the decimal part
before processing)
Arithmetic Operators (unary)
SymbolDescriptionUsageResult
++Pre-increment++ $nIncrements $n by one, then returns $n
++Post-increment$n ++Returns $n, then increments $n by one
--Pre-decrement-- $nDecrements $n by one, then returns $n
--Post-decrement$n --Returns $n, then decrements $n by one
Assignment Operators
SymbolDescriptionUsageResult
=Assignment$n = $mSets value of $n to value of $m
+=Assignment by addition$n += $mSets value of $n to value of $n + $m
(same as $n = $n + $m)
-=Assignment by subtraction$n -= $mSets value of $n to value of $n - $m
(same as $n = $n - $m)
*=Assignment by multiplication$n *= $mSets value of $n to value of $n * $m
(same as $n = $n * $m)
/=Assignment by division$n /= $mSets value of $n to value of $n / $m
(same as $n = $n / $m)
%=Assignment by modulus$n %= $mSets value of $n to value of $n % $m
(same as $n = $n % $m)
Comparison Operators
SymbolDescriptionUsageResult
==Equal$n == $mTRUE if $n is equal to $m
===Identical$n === $mTRUE if $n is equal to $m, and they are
of the same type
!=Not equal$n != $mTRUE if $n is not equal to $m
<>Not equal$n <> $mTRUE if $n is not equal to $m
!==Not identical$n !== $mTRUE if $n is not equal to $m, or they are
not of the same type
<Less than$n < $mTRUE if $n is less than $m
>Greater than$n > $mTRUE if $n is greater than $m
<=Less than or equal to$n <= $mTRUE if $n is less than or equal to $m
>=Greater than or equal to$n >= $mTRUE if $n is greater than or equal to $m
Logical Operators
SymbolDescriptionUsageResult
&&AND$n && $mTRUE if both $n and $m are TRUE
andAND$n and $mTRUE if both $n and $m are TRUE
||OR$n || $mTRUE if either $n or $m is TRUE
orOR$n or $mTRUE if either $n or $m is TRUE
!NOT! $nTRUE if $n is not TRUE
xorExclusive OR (XOR)$n xor $mTRUE if either $n or $m is TRUE (but not both)
Bitwise Operators
SymbolDescriptionUsageResult
&Bitwise AND$n & $mBits that are set in both $n and $m are set
|Bitwise OR$n | $mBits that are set in either $n or $m are set
^Bitwise XOR$n ^ $mBits that are set in either $n or $m (but not in both) are set
~Bitwise NOT~ $nAll bits in $n are inverted
<<Shift left$n << $mShift the bits of $n to the left by $m places (bits shifted off
the left-hand end are discarded, zeros are shifted in on
the right. The sign bit is shifted out, so sign is not preserved).
>>Shift right$n >> $mShift the bits of $n to the right by $m places (bits shifted off
the right-hand end are discarded, and a copy of the sign
bit is shifted in on the left, so sign is preserved.


Operator precedence

Operator precedence determines the order in which expressions are evaluated. For example, in the statement x = 18 + 17 * 2, x evaluates to 52 and not 70 because the multiplication operator (*) has a higher precedence than the addition (+) operator. Parentheses can be used to change precedence if necessary. For example, if we re-write the statement to read x = (18 + 17) * 2, x does evaluate to 70 because expressions inside parentheses are always evaluated first. The table below lists the common operators described above in order of precedence from highest to lowest (operators on the same line have equal precedence).



Operator Precedence
Operator(s)Description
++, --Increment and decrement operators
~Bitwise NOT
!Logical NOT
*, /, %Multiplication, division and modulus
+, -Addition and subtraction
<<, >>Bitwise shift left and shift right
<, <=, >, >=, <> Comparison operators (less than,
less than or equal, greater than,
greater than or equal, not equal)
==, !=, ===, !==Comparison operators (equal, not
equal, identical, not identical)
&Bitwise AND
^Bitwise XOR
|Bitwise OR
&&Logical AND
||Logical OR
=, +=, -=, *=, /=, %=Assignment operators (plain assignment,
assignment by addition, assignment by
subtraction, assignment by multiplication,
assignment by division, assignment by
modulus)
? :The ternary operator
andLogical AND
xorLogical XOR
orLogical OR