Facebook Page Twitter Page LinkedIn Page
× PHP Tutorial PHP Advanced PHP Resources


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
  • Conditional (or Ternary) operators

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
%ModulusReturns remainder of integer division

Example

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

Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and false when values doen 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

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.

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

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

Conditional (or Ternary) operators

The conditional (or ternary) operators are used to set a value depending on conditions.

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.