Operators are utilized to perform operations on variables and values.
PHP categorizes operators into the following groups:
PHP arithmetic operators are used with numeric values to carry out basic arithmetic operations, including addition, subtraction, multiplication, and more.
Operator |
Name |
Example |
Result |
+ |
Addition |
$x + $y |
Sum of $x and $y |
– |
Subtraction |
$x – $y |
Difference of $x and $y |
* |
Multiplication |
$x * $y |
Product of $x and $y |
/ |
Division |
$x / $y |
Quotient of $x and $y |
% |
Modulus |
$x % $y |
Remainder of $x divided by $y |
** |
Exponentiation |
$x ** $y |
Result of raising $x to the $y’th power |
PHP assignment operators are used to assign a value to a variable.
The fundamental assignment operator in PHP is “=”, which means the left operand is set to the value of the expression on the right.
Assignment |
Same as… |
Description |
x = y |
x = y |
The left operand gets set to the value of the expression on the right |
x += y |
x = x + y |
Addition |
x -= y |
x = x – y |
Subtraction |
x *= y |
x = x * y |
Multiplication |
x /= y |
x = x / y |
Division |
x %= y |
x = x % y |
Modulus |
PHP comparison operators are used to compare two values, whether they are numbers or strings.
Operator |
Name |
Example |
Result |
== |
Equal |
$x == $y |
Returns true if $x is equal to $y |
=== |
Identical |
$x === $y |
Returns true if $x is equal to $y, and they are of the same type |
!= |
Not equal |
$x != $y |
Returns true if $x is not equal to $y |
<> |
Not equal |
$x <> $y |
Returns true if $x is not equal to $y |
!== |
Not identical |
$x !== $y |
Returns true if $x is not equal to $y, or they are not of the same type |
> |
Greater than |
$x > $y |
Returns true if $x is greater than $y |
< |
Less than |
$x < $y |
Returns true if $x is less than $y |
>= |
Greater than or equal to |
$x >= $y |
Returns true if $x is greater than or equal to $y |
<= |
Less than or equal to |
$x <= $y |
Returns true if $x is less than or equal to $y |
<=> |
Spaceship |
$x <=> $y |
Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7. |
The PHP increment operators are used to increase a variable’s value.
The PHP decrement operators are used to decrease a variable’s value.
Operator |
Same as… |
Description |
++$x |
Pre-increment |
Increases xxx by one and then returns xxx. |
$x++ |
Post-increment |
Returns xxx, then increases xxx by one. |
–$x |
Pre-decrement |
Decreases xxx by one and then returns xxx. |
$x– |
Post-decrement |
Returns xxx, then reduces xxx by one. |
PHP logical operators are utilized to combine conditional statements.
Operator |
Name |
Example |
Result |
and |
And |
$x and $y |
True if both xxx and yyy are true. |
or |
Or |
$x or $y |
True if either xxx or yyy is true. |
xor |
Xor |
$x xor $y |
True if either xxx or yyy is true, but not both. |
&& |
And |
$x && $y |
True if both xxx and yyy are true. |
|| |
Or |
$x || $y |
True if at least one of xxx or yyy is true. |
! |
Not |
!$x |
True if xxx is false. |
PHP has two operators specifically designed for working with strings.
Operator |
Name |
Example |
Result |
. |
Concatenation |
$txt1 . $txt2 |
The concatenation of txt1\text{txt1}txt1 and txt2\text{txt2}txt2. |
.= |
Concatenation assignment |
$txt1 .= $txt2 |
Appends txt2\text{txt2}txt2 to txt1\text{txt1}txt1. |
PHP array operators.
Operator |
Name |
Example |
Result |
+ |
Union |
$x + $y |
The union of xx and yy can be expressed as x∪ y.
|
== |
Equality |
$x == $y |
Returns true if xxx and yyy contain identical key/value pairs. |
=== |
Identity |
$x === $y |
Returns true if xxx and yyy have identical key/value pairs in the same order and of the same types. |
!= |
Inequality |
$x != $y |
Returns true if xxx is not equal to yyy. |
<> |
Inequality |
$x <> $y |
Returns true if xxx is different from yyy. |
!== |
Non-identity |
$x !== $y |
Returns true if xxx is not identical to yyy. |
The PHP conditional assignment operators are used to assign a value based on specific conditions.
Operator |
Name |
Example |
Result |
?: |
Ternary |
$x = expr1 ? expr2 : expr3 |
Returns the value of xxx. |
?? |
Null coalescing |
$x = expr1 ?? expr2 |
Returns the value of xxx. |