Operator is a symbol that represents an action to be performed on one or more operands. Operands are values or variables on which the operator acts.
PHP supports following type of operators:
It is used to perform common arithmetic operations such as multiplication, subtraction, addition, etc. with numeric values.
Operator | Name | Example | Explanation |
+ | Addintion | $x + $y | Sum of operands |
- | Subtraction | $x - $y | Difference of operands |
* | Multiplication | $x * $y | Product of operands |
/ | Division | $x / $y | Quotient of operands |
% | Modulus | $x % $y | Remainder of operands |
** | Exponentiatio | $x ** $y | $x raised to the power $y |
The Comparison operators are used to compare two values.
Operator | Name | Example | Explanation |
== | Equal | $x == $y | Return TRUE if $x is equal to $y |
=== | identical | $x === $y | Return TRUE if $x is equal to $y, and they are of same data type |
!== | Not identical | $x !== $y | Return TRUE if $x is not equal to $y, and they are not of same data type |
!= | Not equal | $x != $y | Return TRUE if $x is not equal to $y |
<> | Not equal | $x <> $y | Return TRUE if $x is not equal to $y |
< | Less than | $x < $y | Return TRUE if $x is less than $y |
> | Greater than | $x > $y | Return TRUE if $x is greater than $y |
<= | Less than or equal to | $x <= $y | Return TRUE if $x is less than or equal $y |
>= | Greater than or equal to | $x >= $y | Return TRUE if $x is greater than or equal $y |
<=> | Spaceship | $x <=>$y | Return -1 if $x is less than $y Return 0 if $x is equal $y Return 1 if $x is greater than $y |
Logical operators are used to perform logical operations on Boolean values.
Operator | Name | Example | Explanation |
and | And | $x and $y | Return TRUE if both $x and $y are true |
Or | Or | $x or $y | Return TRUE if either $x or $y is true |
xor | Xor | $x xor $y | Return TRUE if either $x or $y is true but not both |
! | Not | ! $x | Return TRUE if $x is not true |
&& | And | $x && $y | Return TRUE if either $x and $y are true |
|| | Or | $x || $y | Return TRUE if either $x or $y |
Assignment operators in PHP are used to assign values to variables. They take the value on the right-hand side and assign it to the variable on the left-hand side.
Operator
| Name | Example | Explanation |
= | Assign
| $x = $y | The value of right operand is assigned to the left operand. |
+= | Add then Assign | $x += $y | Addition same as $a = $x + $y |
-= | Subtract then Assign | $x -= $y | Subtraction same as $x = $x - $y |
*= | Multiply then Assign | $x *= $y | Multiplication same as $x = $x * $y |
/= | Divide then Assign (quotient) | $x /= $y | Find quotient same as $x = $x / $y |
%= | Divide then Assign (remainder) | $x %= $y
| Find remainder same as $x = $x % $y |
Conditional operators, also known as ternary operators, are a concise way to express conditional statements in PHP. It is a shorthand of an if-else statement and are often used for simple decision-making in a single line of code.
Operator | Name | Example | Explanation |
?:
| Ternary | $result = ($condition)? $x_if_true: $y_if_false | Returns the value of $result. The value of $result is $x_if_true if $condition = TRUE. The value of $ result is $y_if_false if $condition = FALSE |
??
| Null coalescing | $result = $x_if_true: $y_if_false | Returns the value of $ result. The value of $result is $x_if_true if $x_if_true exists, and is not NULL. If $x_if_true does not exist, or is NULL, the value of $result is $y_if_false. |
The PHP increment operators are used to increment a variable's value.
The PHP decrement operators are used to decrement a variable's value.
Operator | Name | Example | Explanation |
++$x | Pre-increment | ++$y | Increments $y by one, then returns $y |
$x++ | Post-increment | $y++ | Returns $y, then increments $y by one |
--$x | Pre-decrement | --$y | Decrements $y by one, then returns $y |
$x-- | Post-decrement | $y-- | Returns $y, then decrements $y by one |