What is the null coalescing operator in PHP7?

October 26, 2018

The null coalescing operator is two question marks: ??. It is used to replace some ternary operations. Here is an example:

<?php
// old way
$var = isset($_GET['variable']) ? $_GET['variable'] : "default_val";
// new way - null coalescing:
$var = $_GET["variable"] ?? "default_val";

Both of the above lines do the same thing: they set $var to $_GET['variable'] if it is set (if it passes isset()), but if not then it sets $var to 'default_val'.