5 May 2024
Understanding the different operators in PHP can help you write cleaner, more efficient and more effective code, while also helping you becoming a more skilled and versatile developer. Knowing the different operators in PHP is important for several reasons:Name | Syntax | Example |
Null coalescing | ?? |
$username = $name ?? 'Guest'; |
Null coalesce assignment | ??= |
$username ??= 'Guest'; |
Spacehip | <=> |
$result = $a <=> $b; |
Ternary | ?: |
$result = ($a > $b) ? 'Greater' : 'Lesser or equal'; |
??
) is a relatively new addition to PHP, having been introduced in version 7.0. It allows you to check if a variable exists and is not null, and if it doesn't exist or is null, it will return a default value.
// Without null coalescing.
if (isset($name)) {
$username = $name;
} else {
$username = 'Guest';
}
// Using null coalescing operator.
$username = $name ?? 'Guest';
In the example the null coalescing operator is used to assign the value of $name
to $username
if it exists and is not null. Otherwise the default value of 'Guest'
is assigned.
??=
), which is a combination of the null coalescing operator and the assignment operator (=
). It allows you to assign a value to a variable only if it does not already exist or is null.
// Without null coalesce assignment.
if (!isset($username)) {
$username = 'Guest';
}
// Using null coalesce assignment operator.
$username ??= 'Guest';
In the example the null coalesce assignment operator is used to assign the value of 'Guest'
to $username
only if it does not already exist or is null.
<=>
) is another operator that was introduced in PHP 7.0. It is used to compare two values and return -1, 0, or 1, depending on whether first value is less than, equal to, or greater than the second value.
// Without spaceship.
if ($a > $b) {
$result = 1;
} elseif ($a == $b) {
$result = 0;
} else {
$result = -1;
}
// Using spaceship operator.
$result = $a <=> $b;
In the example the spaceship operator is used to compare the values of $a
and $b
and return the appropriate value. The operator makes the code more concise and easier to read.
?:
) is a shorthand way of writing an if-else statement. It allows you to assign a value to a variable based on a condition.
condition ? value_if_true : value_if_false
Here is an example of the ternary operator in use:
// Without ternary.
if ($a > $b) {
$result = 'Greater';
} else {
$result = 'Lesser or equal';
}
// Using ternary operator.
$result = ($a > $b) ? 'Greater' : 'Lesser or equal';
In the example the ternary operator is used to assign the value of $result
based on the condition $a > $b
. If the condition is true, the value 'Greater'
is assigned, otherwise the value 'Lesser or equal'
is assigned.
$username = $name ? $name : 'Guest'; // Ternary operator.
$username = $name ?: 'Guest'; // Elvis operator.
Both statements assign $name
to the $username
variable if $name
is set. Otherwise the value 'Guest'
is used.
Getting title tag and meta tags from a website using PHP
Published 2024-05-14
PHP
Web development
Learn how to easily webscrape title and meta tags from a website using simple PHP commands.
Read the post →The Seven Restful Controller Actions in Laravel
Published 2024-05-05 — Updated 2024-05-16
Laravel
PHP
Web development
The seven RESTful actions and how you could implement them in Laravel
Read the post →Useful Laravel packages
Published 2024-05-05
Laravel
PHP
Web development
Learn what packages that can help you build even better websites in Laravel. We'll go trhough the must-haves and packages that are just nice to have.
Read the post →