In PHP, operators allows to perform operations on variables or values, according to its data type. PHP has following types of operators.
- Arithmetic Operators
- Assignment Operators
- Logical or Relational Operators
- Comparison Operators
- Conditional or Ternary Operators
- Array Operators
- Increment/Decrement Operators
- String Operators
- Spaceship Operators (Introduced in PHP 7)
- Bitwise operators
Arithmetic Operators
These operators allows to perform mathematical operations
Operator | Operator Name | Example |
---|---|---|
+ | 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.
Operator | Operator Name | Example | Similar to |
---|---|---|---|
= | Equal to | a = 20 | a = 20 |
+= | addition and equals to | a += 20 | a = a + 20 |
-= | subtract and equals to | a -= 20 | a = a – 20 |
*= | Multiply and equals to | a *= 20 | a = a * 20 |
/= | Divide and equals to | a /= 20 | a = a / 20 |
%= | Modulus and equals to | a %= 20 | a = 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.
Operator | Name |
---|---|
++ | Increment operator |
— | Decrement operator |
Example | Description |
---|---|
a++ | Post Increment |
a– | Post decrement |
++a | Pre Increment |
–a | Pre Decrement |
Comparison operators
In PHP, these operators compares two values or variable values, and returns true or false.
Operator | Operator Name |
---|---|
== | Equal |
!= | not equal |
<> | not equal |
=== | Identical |
!== | Not Identical |
> | Greater Than |
< | Less Than |
>= | Less Than or Equal To |
<= | Greater Than or Equal To |
Example
Example | Description |
---|---|
$a == $b | Returns true, if $a & $b are equal |
$a != $b | Returns true, if value of $a & $b are not equal |
$a <> $b | Returns true, if value of $a & $b are not equal |
$a === $b | Returns true, if value and data type of $a & $b are equal |
$a !== $b | Returns true, if value and data type of $a & $b are not equal |
$a > $b | Returns true, if value of $a is greater than $b |
$a < $b | Returns true, if value of $a is less than $b |
$a >= $b | Returns true, , if value of $a is greater than and equals to $b |
$a <= $b | Returns 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.
Operator | Use |
---|---|
and | $a and $b |
or | $a or $b |
xor | $a xor $b |
&& | $a && $b |
|| | $a || $b |
! | !$a |
Example
Example | Result |
---|---|
$a and $b | Returns true, If both $a and $b both are true. |
$a or $b | Returns true, If any $a or $b is true. |
$a xor $b | Returns true, If any $a or $b is true. If $a & $b both are true then it returns false. |
$a && $b | Returns true, If both $a and $b both are true. |
$a || $b | Returns true, If any $a or $b is true. |
!$a | Returns true, If $a is false |
String operators
These operators allows to work with string in PHP.
Operator | Name | Example |
---|---|---|
. | 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
Operator | Name | Result |
---|---|---|
+ | Union | Join two or more arrays |
== | Equality | Returns true, if two arrays key-values are equal |
=== | Identity | Returns true, if two arrays key-values and its type are equal |
!== | Not Identity | Returns true, if two arrays key-values and its type are not equal |
!= | Inequality | Returns true, if two arrays key-values are not equal |
<> | Inequality | Returns true, if two arrays key-values are not equal |
Execution Operators
Operator | Name | Description |
---|---|---|
“ | Backtick | Backticks are used as Execution operators, used to execute shell command |
<?php
echo `dir`;
?>
PHP Conditional Assignment Operators
Operator | Name | Use |
---|---|---|
?: | Ternary | $a = expr1 ? expr2 : expr3 |
?? | Null coalescing | $a = expr1 ?? expr2 |
Example | Result |
---|---|
$a = expr1 ? expr2 : expr3 | if 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.
Example | Result |
---|---|
$a <=> $b | a is greater than b Returns -1 |
$a <=> $b | a is less than b Returns 1 |
$a <=> $b | a is equals to b Returns 0 |
$a <=> $b | a 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
Operator | Name | Description |
---|---|---|
& | And | Set 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 |
~ | Not | Bits that is 0 set to 1 and 1 set to 0. |
<< | Shift left | Bits are left shift of $a and $b |
>> | Shift right | Bits are Right shift of $a and $b |