dtujfmfs06058 2019-01-07 17:37
浏览 82
已采纳

PHP7:如何使用问号(??运算符)作为空变量

Please Check this code:

$title1 = null; // maybe isset or not
$title2 = 'title2'; // maybe isset or not
$title3 = null; // maybe isset or not

$title = $title1 ?? $title2 ?? $title3;

I want print value of non-empty variable.

in this example, with

echo $title; 

I want it print value of $title2 But it print $title1 value.

I can do it with switch of if, But can I do it with ?? method?

if(isset($title1) && $title1)
  $title = $title1;
elseif(isset($title2) && $title2)
  $title = $title2;
elseif(isset($title3) && $title3)
  $title = $title3;
  • 写回答

1条回答 默认 最新

  • doutao6380 2019-01-07 19:35
    关注

    Your code is definitely correct and should work as expected, outputting 'title2'.

    However, your condition isset($title1) && $title1 is not the same. This is more like the 'Elvis' operarator ?: checking for evaluation to boolean, except that the Elvis operator always returns a value (that might be null) and throws a notice on undefined operands. You would have to mute that by the @ operator.

    Your if-else-construct is almost equivalent to

    $title = @$title1 ?: @$title2 ?: @$title3;
    

    The only difference is that $title here is assigned anyway while in your if-else-construct $title is left untouched when all operands are evaluated to false.

    Be aware that muting by the @ operator also prevents critical errors to be shown, e.g. when using a function as operand.

    An alternative is to define your own function. Arguments by value throw a notice on unset variables as well, however, arguments by reference do not. You can design a variadic function taking references:

    function firstNonEmpty(&...$variableRefs)
    {
      $v = null;         // ensure declaration since loop might not interate at all
      foreach ($variableRefs as $v)
        if($v) break;
      return $v ?: null; // we want null even if last value is like false
    }
    
    $c = 'value not considered to be empty';
    $v = firstNonEmpty($a, $b, $c, $d);
    

    Arguments by reference do not accept literals, thus appending some as default argument will not work. However, you can simply use the null-coalescing operator ?? in this case. There is fairly no use-case to add more than one default literal since you know at developtment time whether or not a literal evalualtes to false.

    $v = firstNonEmpty($a, $b, $c, $d, 'default')  ; // does NOT work
    $v = firstNonEmpty($a, $b, $c, $d) ?? 'default'; // DOES work
    
    // One strange thing of PHP is that
    $v = firstNonEmpty($a, ...[0, null, '', 'non-empty']); // DOES work as well
    

    The latter literally creates an array of literals on the fly and references the destructed items. The only thinkable use-case having more than one literal would be generated code by eval.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染