The difference between PHP7's new NULL merge operator?? and ?:

The difference between PHP7's new NULL merge operator?? and ?:

In php7, two interesting operators have been added ?? and ?:

What is the difference between ?? (NULL coalescing operator) and ?:?

The difference between the two operators

(1)?? is as long as the previous variable is defined (that is, isset() is true) or the previous expression is true, then the previous value will be returned.

(2)?: The expression or variable value in front of the operator needs to be true, then the previous value is taken, and if it is false, the latter value is taken. But the premise is that the previous variables need to be defined and already exist.

example:

  echo $i ?? 3. PHP_EOL;
  $arr = [];
  echo $arr['xxx'] ??'yyy';

The above is equivalent to:

  echo isset($arr['xxx'])? $arr['xxx']:'yyy'; 

For ?: is slightly different:

If you open php and report an error, a warning will be generated for undefined variables.

scenes to be used

If you confirm that the variable has been set, it is recommended to use?:

Recommended for other situations?

In the case of uncertain whether the variable exists, there will be cases where the variable expression value is empty and the initial value cannot be accurately set.

Reference: https://cloud.tencent.com/developer/article/1580044 PHP7's new NULL merge operator?? and the difference between ?:-Cloud + Community-Tencent Cloud