MENU
Operators
The operators, in order of decreasing precedence, are:| clone new | clone, new |
| [] | array |
| ++ -- ~ @ (bool) (int) (float) (string) (array) (object) …… |
pre-ncrement, pre-decrement, error suppression type casting |
| instanceof | types |
| ! | logical |
| * / % | arithmetic |
| + - . | arithmetic and string |
| << >> | bitwise |
| < <= > >= | comparison |
| == != === !== <> | comparison |
| & | bitwise and references |
| ^ | bitwise |
| | | bitwise |
| && | logical |
| || | logical |
| ?: | ternary |
| = += -= *= /= .= %= &= |= <<= >>= => | assignment |
| and | logical |
| xor | logical |
| or | logical |
| , | many uses |
| ++ -- | post-increment, post-decrement |
Note the shorthand notations for assignment operators. ($a = $a % 5) is the same as ($a%=5), which is the remainder of $a divided by 5. An assignment evaluates to the value of the assignment, so ($a=$b=5) assign 5 to $a because ($b=5) evaluates to 5.
Placing ++ at the back of a variable increases its value by one after the whole statement has been executed. Placing it in front of a variable increases its value by one before the statement is executed.
The comparison operators are:
| == | equals |
| === | equals in value and type |
| !=,<> | does not equal |
| !== | equals in neither value nor type |
| > | is strictly greater than |
| < | is strictly less than |
| >= | is greater than or equal to |
| <= | is less than or equal to |
Caution must be taken when comparing floats for equality, for the internal representation of floats may offset the precise value slightly.
The logical operators are: !(not), &&(and), ||(or), and, xor and or. Notice that the two variations of the logical operators and and or operate at different precedences. A xor expression evaluates to true only if one of the operands (but not both) evaluates to true. For example,
echo ($b=(”5”==5) xor $d=(“5”===5));
The resulting output is 1 because $b=(”5”==5) evaluates to true while $d=(“5”===5) evaluates to false.
For bitwise operators:
| $a&$b: | 1 for which both corresponding bits are 1s |
| $a|$b: | 1 for which either corresponding bit is 1 |
| $a^$b: | 1 for which either but not both bit is 1 |
| ~$a: | inverts the bits |
| $a >> 3: | shifts the binary form 3 bits to the right |
| $a << 2: | shifts the binary form 2 bits to the left |
To cast a type out of an expression, include the bracketed type in front of the expression:
echo ((int)4.7+1); # 5
echo ((bool)5.7); # 1
The casts available are:
| (int), (integer) | -- integer |
| (bool), (boolean) | -- boolean |
| (float), (double), (real) | -- float |
| (string) | -- string |
| (array) | -- array |
| (object) | -- object |
| (unset) | -- null |
<!DOCTYPE html>
<html><body>
<?php
ini_set("display_errors","on");
echo @(10/0);
$my_file = @file ('non_existent_file') or
die ("Failed opening file");
?>
</body></html><?php
printf("2 ** 3 == %d\n", 2 ** 3);
printf("2 ** 3 ** 2 == %d\n", 2 ** 3 ** 2);
$a = 2;
$a **= 3;
printf("a == %d\n", $a);
?>2 ** 3 == 8
2 ** 3 ** 2 == 512
a == 8
Used directly, returns -1, 0, or 1:
The spaceship operator's most common use is inside a usort() (or similar) callback, since it produces exactly the -1/0/1 result that these functions expect – replacing longer if/elseif chains or subtraction tricks that only work for numbers:
<?php
var_dump(1 <=> 2);
var_dump(2 <=> 2);
var_dump(3 <=> 2);
var_dump("a" <=> "b");
var_dump([1, 2, 3] <=> [1, 2, 3]);
?>int(-1)
int(0)
int(1)
int(-1)
int(0)
<?php
$numbers = [5, 3, 8, 1, 9, 2];
usort($numbers, function ($a, $b) {
return $a <=> $b;
});
echo implode(", ", $numbers) . "\n";
$people = [
["name" => "Charlie", "age" => 35],
["name" => "Alice", "age" => 28],
["name" => "Bob", "age" => 42],
];
usort($people, fn($a, $b) => $a["age"] <=> $b["age"]);
foreach ($people as $person) {
echo $person["name"] . " (" . $person["age"] . ")\n";
}
?>1, 2, 3, 5, 8, 9
Alice (28)
Charlie (35)
Bob (42)
<?php
$data = ["name" => "Alice"];
// ?? returns the left side if it is set and not null, else the right side.
echo ($data["name"] ?? "Anonymous") . "\n";
echo ($data["email"] ?? "no-email@example.com") . "\n";
// Works with undefined array keys/variables without a warning, unlike isset() + ternary.
echo ($undefinedVar ?? "fallback") . "\n";
// Chainable: evaluated left to right, stopping at the first set, non-null value.
$config = [];
echo ($config["timeout"] ?? $data["timeout"] ?? 30) . "\n";
?>Alice
no-email@example.com
fallback
30
<?php
$options = ["color" => "red"];
// ??= assigns only if the key/variable is null or not set.
$options["color"] ??= "blue";
$options["size"] ??= "medium";
echo $options["color"] . "\n";
echo $options["size"] . "\n";
print_r($options);
?>red
medium
Array
(
[color] => red
[size] => medium
)
<?php
class Address {
public string $city = "Paris";
}
class User {
public ?Address $address = null;
}
$userWithAddress = new User();
$userWithAddress->address = new Address();
$userWithoutAddress = new User();
// The whole chain short-circuits to null if any link is null.
echo ($userWithAddress?->address?->city ?? "Unknown") . "\n";
echo ($userWithoutAddress?->address?->city ?? "Unknown") . "\n";
var_dump($userWithoutAddress?->address?->city);
?>Paris
Unknown
NULL
Chaining pipes reads as a left-to-right pipeline instead of nested function calls like strrev(strtoupper($str)).
The right-hand side must be an expression that evaluates to a callable – usually function(...) first-class callable syntax, as below with a user-defined function. A bare function name with no parentheses does not work here, since PHP would try to resolve it as a constant rather than a function reference.
<?php
// The left-hand expression becomes the sole argument to the right-hand callable.
$result = "Hello World" |> strlen(...);
var_dump($result);
// Chaining runs left to right: strtoupper() first, then strrev() on its result.
$shout = "hello world"
|> strtoupper(...)
|> strrev(...);
echo $shout . "\n";
?>int(11)
DLROW OLLEH
<?php
declare(strict_types=1);
function double(int $n): int {
return $n * 2;
}
// Works with any callable, not just built-in functions.
$value = 5 |> double(...) |> double(...);
echo $value . "\n";
?>20