【问题标题】:pass in a ternary operation in php?在php中传递三元运算?
【发布时间】:2015-03-20 14:22:26
【问题描述】:
$foo = false;

$foo ? include 'myfile.php' : ;

不起作用,我应该写什么以避免三元运算中的任何指令过程?

相当于 Python 中的“通过”。

【问题讨论】:

  • 只要使用 if 函数...
  • 这是我对老板说的。

标签: php ternary-operator


【解决方案1】:

没有else就无法构建三元运算符!

你唯一能做的就是让它空着:

$foo ? include 'myfile.php' : '';

但正如 Blaatpraat 所说,使用简单的 IF 语句会更合适

【讨论】:

    【解决方案2】:

    只是为了好玩:

    $foo and include 'myfile.php';
    

    【讨论】:

      【解决方案3】:

      你可以使用又快又脏的 &&:

      $foo && include 'myfile.php';
      

      但如果有一个简单的 if 语句会更清楚:

      if( $foo ) { include 'myfile.php'; }
      

      【讨论】:

        【解决方案4】:

        用这个替换这个:$foo ? include 'myfile.php' : ;

        $included = $foo ? include 'myfile.php' : '';
        

        【讨论】:

        • 可能是因为它是一个小时前发布的答案的信息较少的副本?
        猜你喜欢
        • 2015-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 2017-03-24
        • 2011-12-15
        • 2011-04-04
        • 2012-08-08
        相关资源
        最近更新 更多