php

How to use PHP Null Coalescing Operator

The null coalescing operator (??), one of the new features of PHP 7, can be used as an alternative to the ternary operator and isset() function. It is used to check whether a value is assigned to a variable, and it returns a default value when no value is defined for a variable. This operator can also be used to see if $_GET[] and $_POST[], which receive user inputs, are set. Furthermore, it can check the value of more than one variable via chaining.

In this article, the differences between the ternary and null coalescing operators are discussed, and the null coalescing operator is used as an alternative to the ternary operator and isset() function.

Comparing the ternary operator and the null coalescing operator

The major differences between the ternary operator and the null coalescing operator are described below.

  1. While the null coalescing operator can be used to check the values of two or more variables via chaining, the ternary operator is used to set values for the two variables based on a condition.
  2. While the null coalescing operator can check for NULL values and does not generate an e-notice, the ternary operator cannot check for NULL values and generates an e-notice for a NULL value.
  3. The null coalescing operator returns the value of the variable if that variable contains a value and is not NULL; the ternary operator returns the value from the left of the colon if the condition is true and from the right of the colon if the condition is false.
  4. The null coalescing operator is more readable than the ternary operator.
  5. The null coalescing operator is faster than the ternary operator.

Using the Null Coalescing Operator

The null coalescing operator is used in the following examples.

Example 1: Using the null coalescing operator between two variables

The null coalescing operator can be used with two or more variables. In this example, the operator is used to check the values of different variables.

<?php

//Define two variables
$var1 = 'This is the first string value.';
$var3 = 'This is the third string value.';
$var4 = NULL;
$var5 = 'This is the fifth string value.';
$var6 = '';

//Check the value of the variables
$result1 = $var1 ?? $var2;
echo "<h2 style='color:blue'>$result1</h2>";

//Check the value of the variables
$result2 = $var2 ?? $var3;
echo "<h2 style='color:red'>$result2</h2>";

//Check the value of the variables
$result3 = $var4 ?? $var5;
echo "<h2 style='color:green'>$result3</h2>";

//Check the value of the variables
$result4= $var6 ?? $var5;
echo "<h2 style='color:yellow'>$result4</h2>";

?>

The following output will be generated by running the above script. The value of $var1 is printed in the first output, the value of $var3 is printed in the second output (because $var2 is undefined), the value of $var5 is printed in the third output (because $var4 is NULL), and nothing is printed for the fourth output (because $var6 is empty).


Example 2: Using the null coalescing operator to check $_GET [] and $_POST[] values

In this example, the null coalescing operator is used as an alternative to the isset() function. Here, the first two operators are used to check the value of the $_GET[] array, and the third operator is used to check the $_POST[] array.

<?php

//Check the name value from URL argument
$name = $_GET['name'] ?? 'Unknown';

//Check the email value from URL argument
$email = $_GET['email'] ?? 'No email address is found.';

//Check phone value in $_POST array and in $_GET array
$phone = $_POST['phone'] ?? $_GET['phone'] ?? 'No phone number found.';

//Print the output of the three variables
echo "<b> Name: </b>$name<br /><b> Email: </b>$email<br /><b> Phone: </b>$phone";

?>

The following output will be generated by running the above script without providing values for $_GET[‘name’], $_GET[‘email’], $_GET[‘phone’] and $_POST[‘phone’].


The following output will be generated if you run the script with values for $_GET[‘name’] and $_GET[‘email’]. When the third statement of the script is executed, the value of $_POST[‘phone’], which is undefined, is checked. Next, the value of $_GET[‘phone’], which is also undefined, is checked. Therefore, the default value is printed for the third output.


Example 3: Comparison of the ternary operator to the null coalescing operator

In this example, the Null Coalescing Operator is used to replace the ternary operator. Here, the ternary operator is used to check the value of $_GET[‘n’], and if the variable contains a value, it will store in the variable $number; otherwise, it will print undefined. The same task is done by using the null coalescing operator in the last part of the script.

<?php

echo "<b>The output of ternary operator:</b><br />";
//Using ternary operator to check variable
$number = isset($_GET['n']) ? $_GET['n'] : "undefined";
echo "The value of n is $number <br />";

echo "<b>The output of Null Coalescing Operator:</b><br />";
//Using Null Coalescing Operator to check variable
$number = $_GET['n'] ?? 'undefined';
echo "The value of n is $number <br />";

?>

The following output will be generated if you run the script without providing values for $_GET[‘n’]. Here, the ternary operator checks whether the variable $_GET[‘n’] is set. The outputs of both the ternary operator and the null coalescing operator are undefined because no value is set for $_GET[‘n’].


The following output will be generated if you run the script with a value for $_GET[‘n’]. Both the ternary operator and the null coalescing operator printed the value of $_GET[‘n’] provided in the URL. Here, 100 is assigned for the argument n.

Conclusion

The null coalescing operator is a useful feature of PHP that can be used as an alternative to the ternary operator and the isset() function. It is better than the ternary operator because it is faster and can check the values of multiple variables via chaining, as shown in the second example.

Video Tutorial

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.