Null Coalescing Operator
Last updated onThe PHP null coalescing operator, denoted by ??
, is a convenient feature introduced in PHP 7 that simplifies the way developers handle the default values for variables that may be null.
It provides a concise and readable syntax for dealing with potentially null values without the need for verbose ternary expressions.
Let’s move into its syntax.
Null Coalescing Operator Syntax
The syntax of the PHP null coalescing operator (??
) is quite straightforward. It is used to handle situations where a variable may be null or not set, providing a default value if the variable is null. The basic syntax is as follows:
<?php
$variable = $value ?? $default;
$variable
: The variable you want to check for null.$value
: The value to use if$variable
is not null.$default
: The default value to use if$variable
is null.
The following section provides an overview of how the null coalescing operator works. Let’s proceed.
How does the Null Coalescing Operator work?
This operation occurs in a single line, making the code more readable and concise compared to traditional conditional statements.
Here’s an example that shows you how it works:
<?php
// Using the null coalescing operator
$result = $variable ?? $default;
// Equivalent to
$result = isset($variable) ? $variable : $default;
?>
The operator checks if the variable on the left side ($variable
in this case) is set and is not null. If it is set and not null, the expression evaluates to the value of $variable
. If $variable
is not set or is null, the expression evaluates to the value of $default
.
This operator is especially useful when dealing with user input, configuration settings, or other scenarios where a variable might be null, and you want to provide a default value in case it is.
By moving into the section below, you will learn more about the null coalescing operator in PHP through examples.
Null Coalescing Operator Examples
Here is a basic example:
<?php $username = $_GET['username'] ?? 'Guest'; ?>
In this example, we are trying to retrieve the value of the username
parameter from the $_GET
array. If the username
parameter exists and is not null, it will be assigned to the $username
variable. However, if the username
parameter is null or does not exist, the value ‘Guest’ will be assigned to $username
. This is because of the null coalescing operator.
The null coalescing operator can also be chained together to provide default values for multiple variables. For example:
<?php
$username = $_GET['username'] ?? $_POST['username'] ?? 'Guest';
?>
In this example, we are trying to retrieve the value of the username
parameter from the $_GET
array. If it is null or does not exist, we try to retrieve the value from the $_POST
array. If this value is also null or does not exist, the value ‘Guest’ is assigned to $username
.
The null coalescing operator is particularly useful when dealing with arrays or objects. Here’s an example:
<?php
$person = [
'name' => 'John',
'age' => null,
'country' => 'USA'
];
$age = $person['age'] ?? 'Unknown';
?>
In this example, we are trying to retrieve the value of the age
key from the $person
array. However, this key has a value of null. Using the null coalescing operator, we can assign the value ‘Unknown’ to the $age
variable instead of null.
Moreover, it also can work with functions. For example:
<?php
function getUserName() {
return null;
}
$username = getUserName() ?? 'Guest';
?>
In this example, we are calling the getUserName()
function, which returns null. Using the null coalescing operator, we assign the value ‘Guest’ to $username
instead of null.
Let’s summarize it.
Wrapping UP
The PHP 7 null coalescing operator streamlines the handling of default values for variables that may be null. Its concise and readable syntax simplifies the code, eliminating the need for verbose ternary expressions.
The operator, denoted by ??
, efficiently deals with situations where a variable may be null or unset, providing a default value if needed. The basic syntax involves checking a variable ($variable
) and using a default value ($default
) if the variable is null.
The operator proves beneficial in scenarios such as user input, configuration settings, or any case where a variable might be null, requiring a default value. Its single-line execution enhances code readability and conciseness, offering an alternative to traditional conditional statements.
Further exploration of the null coalescing operator includes chaining for multiple variables and its application with arrays, objects, and functions. Examples demonstrate its versatility, whether handling array elements, object properties, or function returns.