PHP: What is the null coalescing operator in PHP7?
The null coalescing operator is two question marks:
??. It is used to replace some ternary operations. Here is an example:
- // 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'.