×
Author:
Website:
Page title:
URL:
Published:
Last revised:
Accessed:

PHP Operators

Overview

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)

Arithmetic operators are used with numeric values to perform operations such as addition, subtraction, multiplication, division, and so on. The term binary refers to the fact that two operands are involved.



Arithmetic Operators (binary)
SymbolDescriptionUsageResult
+Addition$m + $nSum of $m and $n
-Subtraction$m - $nDifference of $m and $n
*Multiplication$m * $nProduct of $m and $n
/Division$m / $nQuotient of $m and $n (note that a floating-point value is returned unless both operands are integers, and the first operand is divisible by the second)
%Modulus$m % $nRemainder of $m divided by $n (converts floating-point operands to integers by removing the decimal part before processing)
**Exponentiation$m ** $nResult of raising $m to the $nth power

Arithmetic operators (unary)

Unary arithmetic operators are used with numeric values to perform operations such as incrementing and decrementing values. The term unary refers to the fact that only one operand is involved.



Arithmetic Operators (unary)
SymbolDescriptionUsageResult
++Pre-increment++ $mIncrements $m by one, then returns $m
++Post-increment$m ++Returns $m, then increments $m by one
--Pre-decrement-- $mDecrements $m by one, then returns $m
--Post-decrement$m --Returns $m, then decrements $m by one

Assignment operators

Assignment operators are used with numeric values to assign a value to a variable. The basic assignment operator (=) simply assigns the value of the right-hand operand to the left-hand operand. Other assignment operators first perform some mathematical operation involving both operands, such as adding the two operands together, and then assigns the result of that operation to the left-hand operand.



Assignment Operators
SymbolDescriptionUsageResult
=Assignment$m = $nSets value of $m to value of $n
+=Assignment by addition$m += $nSets value of $m to value of $m + $n (same as $m = $m + $n)
-=Assignment by subtraction$m -= $nSets value of $m to value of $m - $n (same as $m = $m - $n)
*=Assignment by multiplication$m *= $nSets value of $m to value of $m * $n (same as $m = $m * $n)
/=Assignment by division$m /= $nSets value of $m to value of $m / $n (same as $m = $m / $n)
%=Assignment by modulus$m %= $nSets value of $m to value of $m % $n (same as $m = $m % $n)

Comparison operators

Comparison operators are used to compare two values - for example two string values or two numeric values – to determine whether or not they have a specific relationship. The result of the comparison is always a Boolean value (TRUE or FALSE).



Comparison Operators
SymbolDescriptionUsageResult
==Equal$m == $nTRUE if $m is equal to $n
===Identical$m === $nTRUE if $m is equal to $n, and they are of the same type
!=Not equal$m != $nTRUE if $m is not equal to $n
<>Not equal$m <> $nTRUE if $m is not equal to $n
!==Not identical$m !== $nTRUE if $m is not equal to $n, or they are not of the same type
<Less than$m < $nTRUE if $m is less than $n
>Greater than$m > $nTRUE if $m is greater than $n
<=Less than or equal to$m <= $nTRUE if $m is less than or equal to $n
>=Greater than or equal to$m >= $nTRUE if $m is greater than or equal to $n
<=>Spaceship$m <=> $nReturns an integer less than, equal to, or greater than zero, depending on whether $m is less than, equal to, or greater than $n

Logical operators

Logical operators are used to test for the existence of a specific logic state, such as whether two expressions both evaluate to TRUE. The result of the test is a Boolean value (TRUE or FALSE).



Logical Operators
SymbolDescriptionUsageResult
&&AND$m && $nTRUE if both $m and $n are TRUE
andAND$m and $nTRUE if both $m and $n are TRUE
||OR$m || $nTRUE if either $m or $n is TRUE
orOR$m or $nTRUE if either $m or $n is TRUE
!NOT! $mTRUE if $m is not TRUE
xorExclusive OR (XOR)$m xor $nTRUE if either $m or $n is TRUE (but not both)

Bitwise operators

Bitwise operators allow evaluation and manipulation of specific bits within an integer.



Bitwise Operators
SymbolDescriptionUsageResult
&Bitwise AND$m & $nBits that are set in both $m and $n are set
|Bitwise OR$m | $nBits that are set in either $m or $n are set
^Bitwise XOR$m ^ $nBits that are set in either $m or $n (but not in both) are set
~Bitwise NOT~ $mAll bits in $m are inverted
<<Shift left$m << $nShift the bits of $m to the left by $n places (bits shifted off the left-hand end are discarded, and zeros are shifted in on the right. The sign bit is shifted out, so sign is not preserved).
>>Shift right$m >> $nShift the bits of $m to the right by $n 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.

String operators

PHP has just two operators that are specifically intended for use with strings - the concatenation operator and the concatenation assignment operator (these work in similar fashion to + and += for numeric values).



String Operators
SymbolDescriptionUsageResult
.Concatenation$str1 . str2Concatenation of $str1 and $str2
.=Concatenation assignment$str1 .= $str2Appends $str2 to $str1

Array operators

Array operators are essentially special comparison operators for use with array variables.



Array Operators
SymbolDescriptionUsageResult
+Union$m + $nUnion of $m and $n
==Equality$m == $nReturns TRUE if $m and $n have the same key/value pairs
===Identity$m === $nReturns true if $m and $n have the same key/value pairs in the same order and of the same types
!=Inequality$m != $nReturns true if $m is not equal to $n
<>Inequality$m <> $nReturns true if $m is not equal to $n
!==Non-identity$m !== $nReturns true if $m is not identical to $n

Conditional assignment operators

A conditional assignment operator will return one of two possible values, depending on whether or not the specified condition has been met.



Conditional Assignment Operators
SymbolDescriptionUsageResult
? :Ternary$m = expr1 ? expr2 : expr3Returns the value of $m
The value of $m is expr2 if expr1 = TRUE
The value of $m is expr3 if expr1 = FALSE
??Null coalescing$m = expr1 ?? expr2Returns the value of $m
The value of $m is expr1 if expr1 exists, and is not NULL
If expr1 does not exist, or is NULL, the value of $m is expr2

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
**Exponentiation
++, --Increment and decrement operators
~Bitwise NOT
!Logical NOT
*, /, %Multiplication, division and modulus
+, -Addition and subtraction
<<, >>Bitwise shift left and shift right
:String concatenation
<, <=, >, >=Comparison operators (less than, less than or equal, greater than, greater than or equal)
==, !=, ===, !==, <>, <=>Comparison operators (equal, not equal, identical, not identical, not equal, spaceship)
&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, string concatenation assignment)
? :The ternary operator
andLogical AND
xorLogical XOR
orLogical OR