What does double question mark (??) operator mean in PHP ?


PHP 7 has added a new operator double question mark (??) operator. In PHP 7, the double question mark(??) operator known as Null Coalescing Operator.

It returns its first operand if it exists and is not NULL; otherwise, it returns its second operand. It evaluates from left to right. Null Coalescing operator also can be used in a chain format.

Let's take the below example to demonstrate the double question mark (??) operator.

Example

<?php
   //$a is not set
   echo $a ?? 9 ??45;
?>

Output

9

Example

<?php
   //$a is not set
   $b = 34;
   echo $a ?? $b ?? 7;
?>

Output

34

Updated on: 29-Jun-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements