An operator takes one or more expressions and results in a value.
+ - * / % **= += -= *= /= %= **=?: ??++ --&& || ! and or xor. .=Note; You can check Operator Precedence here.
Arithmetic operators can perform mathematical operations + - * / % **:
$x = 8;
$y = 2;
var_dump($x + $y); // int(10)
var_dump($x - $y); // int(6)
var_dump($x * $y); // int(16)
var_dump($x / $y); // int(4)
var_dump($x % $y); // int(0)
var_dump($x ** $y); // int(64)
$x = +"8";
$y = - "2";
var_dump($x); // int(8)
var_dump($y); // int(-2)
$x = 10;
$y = 3;
var_dump($x / $y); // float(3.3333333333)
fdiv() function.$x = 7;
$y = 0;
var_dump(fdiv($x, $y)); // float(INF)
$x = 8.5;
$y = 3.2;
var_dump($x % $y); // int(2)
The PHP assignment operators write a value to a variable. They include = += -= *= /= %= **=.
| Assignment | Similar to |
|---|---|
| x = y | x = y |
| x += y | x = x + y |
| x -= y | x = x - y |
| x *= y | x = x * y |
| x /= y | x = x / y |
| x %= y | x = x % y |
$x = 8;
$x += 2;
echo $x; // 10
We can use comparison operators to compare two values (number or string):
| Operator | Name |
|---|---|
| == | Equal |
| === | Identical |
| != | Not equal |
| <> | Not equal |
| !== | Not identical |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| <=> | Spaceship |
$x = 8;
$y = 2;
var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x !== $y);
var_dump($x <=> $y);
We can use conditional operators to set a value depending on conditions.
| Operator | Name | Example |
|---|---|---|
| ?: | Ternary | $x = a ? b : c |
| ?? | Null coalescing | $x = a ?? b |
The ternary operator returns b if a is true, c if a is false.
$x = 26;
$result = $x >= 18 ? "allow": "not allow";
echo $result; // allow
Null coalescing is mainly used when working with null. It returns b if a is null.
$x = null;
$y = $x ?? "Hello";
echo $y; // Hello
We can use increment and decrement operators to increase or decrease a variable’s value.
| Operator | Description |
|---|---|
| $x++ | Returns $x, then increments $x by one |
| $x– | Returns $x, then decrements $x by one |
| ++$x | Increments $x by one, then returns $x |
| –$x | Decrements $x by one, then returns $x |
$x = 10;
echo $x++; // 10
echo $x; // 11
echo ++$x; // 12;
echo --$x; // 11;
We can use logical operators to combine conditional statements.
| Operator | Result |
|---|---|
| && | True if both $x and $y are true |
| || | True if either $x or $y is true |
| ! | True if $x is not true |
| and | True if both $x and $y are true |
| or | True if either $x or $y is true |
| xor | True if either $x or $y is true, but not both |
$x = true;
$y = false;
var_dump($x && $y); // bool(false)
var_dump($x || $y); // bool(true)
var_dump(!$x); // bool(false)
We can use string operators . .= for concatenation.
$x = "Hello";
$x .= "World";
echo $x; // HelloWorld
| Operator | Name | Result |
|---|---|---|
| + | Union | Union of $x and $y |
| == | Equality | True if $x and $y have same key/value pairs |
| === | Identity | True if $x and $y have the same key/value pairs same order + types |
| != | Inequality | True if $x is not equal to $y |
| <> | Inequality | True if $x is not equal to $y |
| !== | Non-identity | True if $x is not identical to $y |
$x = ["a" => 1, "b" => 2];
$y = ["d" => 3, "e" => 4, "f" => 5];
echo "<pre>";
print_r($x+$y);
echo "</pre>";
/*
Array
(
[a] => 1
[b] => 2
[d] => 3
[e] => 4
[f] => 5
)
*/