PHP Tutorial PHP Advanced PHP References

PHP - Operators



Operators are used to perform operation on a single operand or two operands. Operators in PHP can be categorized as follows:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • Bitwise operators
  • Miscellaneous operators

PHP Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands.

OperatorNameDescription
+AdditionAdd two values
-SubtractionSubtract one value from another
*MultiplicationMultiply two values
/DivisionDivide one value by another
**Exponent / PowerReturns first operand raised to the power of second operand
%ModuloReturns remainder of integer division

Example

PHP Assignment operators

Assignment operators are used to assign values of right hand side expression to left hand side operand.

Operator Expression Equivalent toExample
=a = 5a = 5Example
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
/=a /= ba = a / b
**=a **= ba = a ** b
%=a %= ba = a % b
&=a &= ba = a & bMore Info
|=a |= ba = a | bMore Info
^=a ^= ba = a ^ bMore Info
>>=a >>= ba = a >> bMore Info
<<=a <<= ba = a << bMore Info

PHP Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.

Operator Description
==Equal: Checks the values of two operands and returns true if they are same.
!=Not equal: Checks the values of two operands and returns true if they are not same.
<>
===Identical: Checks the values and datatypes of two operands and returns true if they are same.
!==Not identical: Checks the values and datatypes of two operands and returns true if they are not same.
>Greater than: Checks the values of two operands and returns true if the value of first operand is greater than the value of second operand.
<Less than: Checks the values of two operands and returns true if the value of first operand is less than the value of second operand.
>=Greater than or equal to: Checks the values of two operands and returns true if the value of first operand is greater than or equal to the value of second operand.
<=Less than or equal to: Checks the values of two operands and returns true if the value of first operand is less than or equal to the value of second operand.
<=>Spaceship: Checks the values of two operands and returns values based on the values of two operands:
  • Returns -1 if the value of first operand is less than value of second operand.
  • Returns 0 if the values of two operands are equal.
  • Returns 1 if the value of first operand is greater than value of second operand.

Example

PHP Increment/Decrement operators

Increment and decrement operators are used to increase and decrease the value of variable.

OperatorNameDescriptionExample
++$xPre-incrementIncreases the value of $x by 1, then returns $x.Example
$x++Post-incrementReturns $x, then increases the value of $x by 1.
--$xPre-decrementDecreases the value of $x by 1, then returns $x.Example
$x--Post-decrementReturns $x, then decreases the value of $x by 1.

PHP Logical operators

Logical operators are used to combine two or more conditions.

Operator NameDescription
and ANDReturns true when all conditions are true
&&
orORReturns true when any of the conditions is true
||
xorExclusive ORReturns true when any of the conditions is true, but not both
!NOTReturns true when the given conditions is not true

More Info

PHP Bitwise operators

Bitwise operators are used to perform bitwise operations on two operands.

OperatorNameDescriptionMore Info
& ANDReturns 1 if both bits at the same position in both operands are 1, else returns 0More Info
|ORReturns 1 if one of two bits at the same position in both operands is 1, else returns 0More Info
^XORReturns 1 if only one of two bits at the same position in both operands is 1, else returns 0More Info
~ NOTReverse all the bitsMore Info
>>Right shiftThe left operand is moved right by the number of bits present in the right operandMore Info
<<Left shiftThe left operand value is moved left by the number of bits present in the right operandMore Info

PHP Miscellaneous operators

The table below describes other operators supported by PHP:

Operator NameExample
?: Ternary $x = exp1 ? exp2 : exp3
  • The value of $x is exp2 if exp1 is TRUE.
  • The value of $x is exp3 if exp1 is FALSE.
?? Null coalescing $x = exp1 ?? exp2
  • The value of $x is exp1 if exp1 exists and not NULL.
  • The value of $x is exp2 if exp1 does not exist, or is NULL.

PHP Operators Precedence

Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.

The following table lists the precedence and associativity of PHP operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.

PrecedenceOperatorDescriptionAssociativity
25cloneclone operatorNA
newnew operator
24**ExponentiationRight to Left
23++a  --aPrefix increment, Prefix decrementNA
a++  a--Postfix increment, Postfix decrement
+a  -aUnary plus, Unary minus
~Bitwise NOT
(int)  (float)  (string)  (array)  (object)  (bool)Type casting
@Error control operator
22instanceofType operatorLeft to Right
21!Logical NOTNA
20*  /  %Multiplication, Division, RemainderLeft to Right
19+  -  .Addition, Subtraction, Concatenating operator (. prior to PHP 8.0.0)
18<<  >>Bitwise left shift and right shift
17.Concatenating operator (as of PHP 8.0.0)
16<  <=  >  >=Less than, Less than or equal, Greater than, and Greater than or equalnon-associative
15==  !=Equality, Inequality
===  !==Strict Equality, Strict Inequality
<>  <=>Inequality, Spaceship operator
14&Bitwise AND and reference operatorLeft to Right
13^Bitwise XOR
12|Bitwise OR
11&&Logical AND
10||Logical OR
9??Null coalescing operatorRight to Left
8a?b:cConditional (ternary) operatorNon-associative (Left to Right associative prior to PHP 8.0.0)
7=Direct assignmentRight to Left
+=  -=  *=  /=  %=  **=Compound assignment by addition, subtraction, multiplication, division, remainder, and exponentiation
.=  ??=Concatenating assignment operator, Null coalescing assignment operator
<<=  >>=Compound assignment by Bitwise left shift and right shift
&=  ^=  |=Compound assignment by Bitwise AND, XOR and OR
6yield fromyield fromNA
5yieldyield
4printprint
3andLogical ANDLeft to Right
2xorLogical XOR
1orLogical OR