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

Operator Name Example
+ Addition 2 + 4
Subtraction 6 – 2
* Multiplication 5 * 3
/ Division 15 / 3
% Modulus 43 % 10
** Exponentiation 10**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 “=”.

Operator Name Example Equivalent 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.

Operator Name Example Result
== Equal To $x == $y false
!= Not Equal To $x != $y true
< Less Than $x < $y true
> Greater Than $x > $y false
<= Less Than or Equal To $x <= $y true
>= Greater Than or Equal To $x >= $y false

 

Increment/Decrement operators

Operator Name Example Explanation
++ Increment ++$a Increment the value of $a by one, then return $a
$a++ Return $a, then increment the value of $a by one
decrement –$a Decrement the value of $a by one, then return $a
$a– Return $a, then decrement the value of $a by one

 

Logical Operators

Operator Name Example Explanation
and And $a and $b Return TRUE if both $a and $b are true
Or Or $a or $b Return TRUE if either $a or $b is true
xor Xor $a xor $b Return TRUE if either $ or $b is true but not both
! Not ! $a Return TRUE if $a is not true
&& And $a && $b Return TRUE if either $a and $b are true
|| Or $a || $b Return TRUE if either $a or $b is true

 

String Operators

Operator Name Example Explanation
. Concatenation $a . $b Concatenate both $a and $b
.= Concatenation and Assignment $a .= $b First concatenate $a and $b, then assign the concatenated string to $a, e.g. $a = $a . $b

 

Array operators

Operator Name Example Explanation
+ Union $a + $y Union of $a and $b
== Equality $a == $b Return TRUE if $a and $b have same key/value pair
!= Inequality $a != $b Return TRUE if $a is not equal to $b
=== Identity $a === $b Return TRUE if $a and $b have same key/value pair of same type in same order
!== Non-Identity $a !== $b Return TRUE if $a is not identical to $b
<> Inequality $a <> $b Return TRUE if $a is not equal to $b