Best way to give a variable a default value (simulate Perl ||, ||= )

后端 未结 8 1383
眼角桃花
眼角桃花 2020-12-07 11:46

I love doing this sort of thing in Perl: $foo = $bar || $baz to assign $baz to $foo if $bar is empty or undefined. You al

8条回答
  •  执笔经年
    2020-12-07 12:16

    In PHP earlier than 7.*, one may use ?: for an undefined variable having errors locally suppressed with an @:

    $foo = @$bar ?: $baz;
    

提交回复
热议问题