dongmao7195 2019-02-26 08:44
浏览 38
已采纳

是否可以使用null合并运算符执行此操作?

With isset(), you can do this:

$foo = [];
$baz = [];
$bar = isset($foo['bar'], $baz[$foo['bar']]) ? $baz[$foo['bar']] : '';

Is it possible to achieve the same thing with null coalescing like:

$bar = ($foo[$bazz['foo']]) ?? ''; //Undefined variable: bazz

Im expecting $bar to be:

$bar = '';

??: Null coalescing operator

  • 写回答

3条回答 默认 最新

  • doudihuang7642 2019-02-26 09:18
    关注

    You have to use the ?? operator twice.

    $foo = [];
    $baz = [];
    $bar = $baz[$foo['bar'] ?? ''] ?? '' ;
    

    See it in action on 3v4l.org

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?