PHP Operator is a symbol used to perform operations on operands. In simple words, operators are used to perform operations on variables or values.
Arithmetic Operators
The PHP arithmetic operators are used to perform common arithmetic operations such as addition, subtraction, etc. with numeric values.
Operator | Name | Example | Explanation |
+ | Addition | $a + $b | Sum of operands |
– | Subtraction | $a – $b | Difference of operands |
* | Multiplication | $a * $b | Product of operands |
/ | Division | $a / $b | Quotient of operands |
% | Modulus | $a % $b | Remainder of operands |
** | Exponentiation | $a ** $b | $a raised to the power $b |
Assignment Operators
The assignment operators are used to assign value to different variables. The basic assignment operator is “=”.
Operator | Name | Example | Explanation |
= | Assign | $a = $b | The value of the right operand is assigned to the left operand. |
+= | Add then Assign | $a += $b | Addition same as $a = $a + $b |
-= | Subtract then Assign | $a -= $b | Subtraction same as $a = $a – $b |
*= | Multiply then Assign | $a *= $b | Multiplication same as $a = $a * $b |
/= | Divide then Assign(quotient) | $a /= $b | Find quotient same as $a = $a / $b |
%= | Divide then Assign(remainder) | $a %= $b | Find remainder same as $a = $a % $b |
Bitwise Operators
The bitwise operators are used to perform bit-level operations on operands. These operators allow the evaluation and manipulation of specific bits within the integer.
Operator | Name | Example | Explanation |
& | And | $a & $b | Bits that are 1 in both $a and $b are set to 1, otherwise 0. |
| | Or (Inclusive or) | $a | $b | Bits that are 1 in either $a or $b are set to 1 |
^ | Xor (Exclusive or) | $a ^ $b | Bits that are 1 in either $a or $b are set to 0. |
~ | Not | ~$a | Bits that are 1 set to 0 and bits that are 0 are set to 1 |
<< | Shift left | $a << $b | Left shift the bits of operand $a $b steps |
>> | Shift right | $a >> $b | Right shift the bits of $a operand by $b number of places |
Comparison Operators
Comparison operators allow comparing two values, such as number or string. Below the list of comparison operators are given:
Operator | Name | Example | Explanation |
== | Equal | $a == $b | Return TRUE if $a is equal to $b |
=== | Identical | $a === $b | Return TRUE if $a is equal to $b, and they are of same data type |
!== | Not identical | $a !== $b | Return TRUE if $a is not equal to $b, and they are not of same data type |
!= | Not equal | $a != $b | Return TRUE if $a is not equal to $b |
<> | Not equal | $a <> $b | Return TRUE if $a is not equal to $b |
< | Less than | $a < $b | Return TRUE if $a is less than $b |
> | Greater than | $a > $b | Return TRUE if $a is greater than $b |
<= | Less than or equal to | $a <= $b | Return TRUE if $a is less than or equal $b |
>= | Greater than or equal to | $a >= $b | Return TRUE if $a is greater than or equal $b |
<=> | Spaceship | $a <=>$b | Return -1 if $a is less than $bReturn 0 if $a is equal $bReturn 1 if $a is greater than $b |
Incrementing/Decrementing Operators
The increment and decrement operators are used to increase and decrease the value of a variable.
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
The logical operators are used to perform bit-level operations on operands. These operators allow the evaluation and manipulation of specific bits within the integer.
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
The string operators are used to perform the operation on strings. There are two string operators in PHP, which are given below:
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
The array operators are used in case of array. Basically, these operators are used to compare the values of arrays.
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 |