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

后端 未结 8 1386
眼角桃花
眼角桃花 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:36

    this is another good format for the isset case

    isset($foo) || $foo= $bar;

    another simple way and will give you more control as you can add more conditions and assign to another variable in the same time

    $foo = (isset($oData['foo']))?$bar['foo']:'default value';

提交回复
热议问题