Current Location: Home> Latest Articles> Implement PHP7 'Null Coalescing' Syntax Sugar in Two Lines of Code to Improve Code Readability

Implement PHP7 'Null Coalescing' Syntax Sugar in Two Lines of Code to Improve Code Readability

gitbox 2025-06-28

Introduction

PHP7 is a widely used open-source server-side scripting language known for its simplicity, ease of learning, and powerful features. With the release of PHP7, many useful features and syntax sugars were introduced to help developers write more efficient code. One of these features is the Null Coalescing Operator (??), which simplifies code and reduces unnecessary conditional checks.

Principle of Null Coalescing Operator

The Null Coalescing Operator (??) in PHP7 is used to check if a variable is null, and return a non-null value. If the variable is not null, it returns the variable's value; if the variable is null, it returns the value following the ?? operator. This reduces the need for verbose conditional statements.

How to Use the Null Coalescing Operator

Using the Null Coalescing Operator is simple. You just add ?? after a variable and then specify a default value. For example:

<span class="fun">$result = $variable ?? $default;</span>

In this code, $variable is the variable being checked, and $default is the value to be used if $variable is null. If $variable is not null, $result will take the value of $variable; if $variable is null, $result will take the value of $default.

Adding 'Null Coalescing' Syntax Sugar

To make the use of the Null Coalescing Operator even more intuitive and concise, we can add a 'null coalescing' syntax sugar with just two lines of code.

Implementation

Here is how to add 'null coalescing' syntax sugar to PHP7 with just two lines of code:

if (!function_exists('ncoalesce')) {
    function ncoalesce($var, $default) {
        return $var ?? $default;
    }
}

In the code above, we first check whether the ncoalesce() function has already been defined using function_exists(). If it has not been defined, we define the ncoalesce() function. This function simply returns either $var or $default using the Null Coalescing Operator.

Usage Example

Once the null coalescing syntax sugar is added, we can use the ncoalesce() function like any other function to concisely check if a variable is null and return a default value. Here's an example:

$name = ncoalesce($_POST['name'], 'Guest');
echo "Hello, $name!";

In this example, the ncoalesce() function checks if $_POST['name'] is null. If it is, it returns the default value 'Guest'; otherwise, it returns the value of $_POST['name'].

Conclusion

With just two simple lines of code, we have successfully added a 'null coalescing' syntax sugar to PHP7, making the Null Coalescing Operator even more concise and intuitive. This not only improves code readability but also avoids verbose conditional statements, greatly enhancing development efficiency. In real-world development, the Null Coalescing Operator is especially useful when handling form data, configuration options, and other similar cases. By using this simple syntax sugar, you can make your PHP code clearer and more maintainable.