【问题标题】:Break down ternary operator into if statment? [closed]将三元运算符分解为 if 语句? [关闭]
【发布时间】:2013-07-01 02:28:34
【问题描述】:

我有这样的代码:

$get_options = isset($options['big_heading']) ? ($options['big_heading']) : '' ; 但我很难将其作为三元运算符阅读,那么我该如何将其分解为 if 语句?对不起我的菜鸟问题... 谢谢!!

【问题讨论】:

  • 嗯,使用三元运算符比 if/else 语句更好,而且可读性更好。嗯,这是主观的。
  • 这里发生了什么垃圾,你认为这是问这个问题的正确空间。 :(
  • 好吧,Neeraj Singh 并不是每个人都像你一样了解 PHP,所以我们中的一些人仍在学习这些东西......

标签: php conditional-operator


【解决方案1】:

您的代码等于:

if(isset( $options['big_heading'])){
    $get_options = $options['big_heading'];
} else {
    $get_options = '';
}

这个语法总是这样的:$foo = (condition) ? if_its_true : if_its_not_true ;

【讨论】:

    【解决方案2】:

    这和

    一样
    $get_options = isset( $options['big_heading']) ? ($options['big_heading']) : '' ;
    

    这个

    if(isset( $options['big_heading'])) {
        $get_options = $options['big_heading'];
    } else {
        $get_options = '';
    }
    

    【讨论】:

      【解决方案3】:
      if(isset( $options['big_heading'])) {
        $get_options = $options['big_heading'];
      } else {
        $get_options = '';
      }
      

      【讨论】:

        【解决方案4】:
        $get_options = "";
        
        if(isset( $options['big_heading']))
        {
           $get_options = $options['big_heading']
        }
        

        【讨论】:

          【解决方案5】:
          $get_options =  '' ;
          if (isset( $options['big_heading'])) {
           $get_options = $options['big_heading'];
          }
          

          【讨论】:

            【解决方案6】:

            给你...

            if (isset($options['big_heading'])){
                $get_options = $options['big_heading'];
            else{
                $get_options = '';
            }
            

            【讨论】:

              猜你喜欢
              • 2018-12-26
              • 1970-01-01
              • 2021-07-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-02-12
              • 1970-01-01
              • 2017-10-20
              相关资源
              最近更新 更多