In PHP, operators allows to perform operations on variables or values, according to its data type. PHP has following types of operators.

  1. Arithmetic Operators
  2. Assignment Operators
  3. Logical or Relational Operators
  4. Comparison Operators
  5. Conditional or Ternary Operators
  6. Array Operators
  7. Increment/Decrement Operators
  8. String Operators
  9. Spaceship Operators (Introduced in PHP 7)
  10. Bitwise operators

Arithmetic Operators

These operators allows to perform mathematical operations

OperatorOperator NameExample
+Addition$a + $b
Subtraction$a – $b
*Multiplication$a * $b
/Multiplication$a / $b
%Modulus$a % $b
**Exponentiation$a ** $b

Assignment Operators

This is equals to symbol called assignment operators, use to assign value to a variable.

OperatorOperator NameExampleSimilar to
=Equal toa = 20a = 20
+=addition and equals toa += 20 a = a + 20
-=subtract and equals toa -= 20a = a – 20
*=Multiply and equals toa *= 20a = a * 20
/=Divide and equals toa /= 20a = a / 20
%=Modulus and equals toa %= 20a = a % 20

Increment/Decrement operators

These operators increases or decrease a variable value by 1. It can be pre-increment or pre-decrement or post-increment or post-decrement.

OperatorName
++Increment operator
Decrement operator
ExampleDescription
a++Post Increment
a–Post decrement
++aPre Increment
–aPre Decrement

Comparison operators

In PHP, these operators compares two values or variable values, and returns true or false.

OperatorOperator Name
==Equal
!=not equal
<>not equal
===Identical
!==Not Identical
>Greater Than
<Less Than
>=Less Than or Equal To
<=Greater Than or Equal To

Example

ExampleDescription
$a == $b
Returns true, if $a & $b are equal
$a != $bReturns true, if value of $a & $b are not equal
$a <> $bReturns true, if value of $a & $b are not equal
$a === $bReturns true, if value and data type of $a & $b are equal
$a !== $bReturns true, if value and data type of $a & $b are not equal
$a > $bReturns true, if value of $a is greater than $b
$a < $bReturns true, if value of $a is less than $b
$a >= $bReturns true, , if value of $a is greater than and equals to $b
$a <= $bReturns true, , if value of $a is less than and equals to $b

PHP Logical Operators

Logical operators allows to compare multiple conditions with comparison operators.

OperatorUse
and$a and $b
or$a or $b
xor$a xor $b
&&$a && $b
||$a || $b
!!$a

Example

ExampleResult
$a and $bReturns true, If both $a and $b both are true.
$a or $bReturns true, If any $a or $b is true.
$a xor $bReturns true, If any $a or $b is true. If $a & $b both are true then it returns false.
$a && $bReturns true, If both $a and $b both are true.
$a || $bReturns true, If any $a or $b is true.
!$aReturns true, If $a is false

String operators

These operators allows to work with string in PHP.

OperatorNameExample
.Concatenation$a . $b
.=Concatenate and assign$a .= $b same as $a = $a . $b

PHP Array Operators

Array operators allows to perform various operation on array like

OperatorNameResult
+UnionJoin two or more arrays
==EqualityReturns true, if two arrays key-values are equal
===IdentityReturns true, if two arrays key-values and its type are equal
!==Not IdentityReturns true, if two arrays key-values and its type are not equal
!=InequalityReturns true, if two arrays key-values are not equal
<>InequalityReturns true, if two arrays key-values are not equal

Execution Operators

OperatorNameDescription
BacktickBackticks are used as Execution operators, used to execute shell command
<?php
echo `dir`;
?>

PHP Conditional Assignment Operators

OperatorNameUse
?:Ternary$a = expr1 ? expr2 : expr3
??Null coalescing$a = expr1 ?? expr2
ExampleResult
$a = expr1 ? expr2 : expr3if expr1 is true, value of $a is expr2
if expr1 is false, value of $a is expr3
$a = expr1 ?? expr2$a = expr1
if expr1 exists and not null.

$a = expr2
if expr1 does not exists or null

Spaceship operators

It is denoted by <=> introduced in PHP 7, It is also used to compare values but it returns integer instead of Boolean true or false. <=> is spaceship operator.

ExampleResult
$a <=> $ba is greater than b Returns -1
$a <=> $ba is less than b Returns 1
$a <=> $ba is equals to b Returns 0
$a <=> $ba is not equals to b Returns 0

Type Operators

instanceof is used as Type operators which tells that a object belongs to the class or not.

<?php
class class1
{
}

class class2
{
}
$o = new class1;

var_dump($o instanceof class1);
var_dump($o instanceof class2);
?>

Bitwise Operator

OperatorNameDescription
&AndSet bits to 1 if both bits are 1 in variable $a, $b, else set to 0.
|Or (inclusive or) Set to 1 if any of bit is 1.
^Xor (exclusive or) Set bits to 0, if any of bit is 1
~NotBits that is 0 set to 1 and 1 set to 0.
<<Shift leftBits are left shift of $a and $b
>>Shift rightBits are Right shift of $a and $b