Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators

 

Arithmetic operators

OperatorNameExample
+Addition2 + 4
Subtraction6 – 2
*Multiplication5 * 3
/Division15 / 3
%Modulus43 % 10
**Exponentiation10**3 = 1000

NOTE: The exponentiation (**) operator has been introduced in PHP 5.6.

 

Assignment operators

The assignment operators are used to assign value to different variables. The basic assignment operator is “=”.

OperatorNameExampleEquivalent Operation
+=Plus Equals$x += 2;$x = $x + 2;
-=Minus Equals$x -= 4;$x = $x – 4;
*=Multiply Equals$x *= 3;$x = $x * 3;
/=Divide Equals$x /= 2;$x = $x / 2;
%=Modulo Equals$x %= 5;$x = $x % 5;
.=Concatenate Equals$my_str.=”hello”;$my_str = $my_str . “hello”;

 

Comparison operators

Comparisons are used to check the relationship between variables and/or values.

OperatorNameExampleResult
==Equal To$x == $yfalse
!=Not Equal To$x != $ytrue
<Less Than$x < $ytrue
>Greater Than$x > $yfalse
<=Less Than or Equal To$x <= $ytrue
>=Greater Than or Equal To$x >= $yfalse

 

Increment/Decrement operators

OperatorNameExampleExplanation
++Increment++$aIncrement the value of $a by one, then return $a
$a++Return $a, then increment the value of $a by one
decrement–$aDecrement the value of $a by one, then return $a
$a–Return $a, then decrement the value of $a by one

 

Logical Operators

OperatorNameExampleExplanation
andAnd$a and $bReturn TRUE if both $a and $b are true
OrOr$a or $bReturn TRUE if either $a or $b is true
xorXor$a xor $bReturn TRUE if either $ or $b is true but not both
!Not! $aReturn TRUE if $a is not true
&&And$a && $bReturn TRUE if either $a and $b are true
||Or$a || $bReturn TRUE if either $a or $b is true

 

String Operators

OperatorNameExampleExplanation
.Concatenation$a . $bConcatenate both $a and $b
.=Concatenation and Assignment$a .= $bFirst concatenate $a and $b, then assign the concatenated string to $a, e.g. $a = $a . $b

 

Array operators

OperatorNameExampleExplanation
+Union$a + $yUnion of $a and $b
==Equality$a == $bReturn TRUE if $a and $b have same key/value pair
!=Inequality$a != $bReturn TRUE if $a is not equal to $b
===Identity$a === $bReturn TRUE if $a and $b have same key/value pair of same type in same order
!==Non-Identity$a !== $bReturn TRUE if $a is not identical to $b
<>Inequality$a <> $bReturn TRUE if $a is not equal to $b