【问题标题】:PHP switch case more than one value in the casePHP switch case 中多个值的情况
【发布时间】:2011-05-08 23:17:47
【问题描述】:

我有一个变量保存值“每周”、“每月”、“季度”和“年度”,我还有另一个变量保存从 1 到 10 的值。

switch ($var2) {
    case 1:
        $var3 = 'Weekly';
        break;
    case 2:
        $var3 = 'Weekly';
        break;
    case 3:
        $var3 = 'Monthly';
        break;
    case 4:
        $var3 = 'Quarterly';
        break;
    case 5:
        $var3 = 'Quarterly';
        break;
    // etc.
}

它并不漂亮,因为我的代码有很多重复项。我想要什么:

switch ($var2) {
    case 1, 2:
        $var3 = 'Weekly';
        break;
    case 3:
        $var3 = 'Monthly';
        break;
    case 4, 5:
        $var3 = 'Quarterly';
        break;
}

我如何在 PHP 中做到这一点?

【问题讨论】:

    标签: php switch-statement case


    【解决方案1】:
    function bankRemark()
    {
        
        $this->db->select('id,status,funding_dt,date,remarks1');
        $this->db->from($this->db_sdip);       
        $this->db->where("amc_remark != '' ");      
    
    
        $query = $this->db->get();  
    
        // echo $this->db->last_query();die;
    
        if($query->num_rows() > 0)
        {
            $data = $query->result();    
            foreach($data as $val)
            {
                $id         = $val->id;
                $status     = strtoupper($val->status);
                $funding_dt = $val->funding_dt;
                $date       = $val->date;
                $remarks1   = $val->remarks1;
    
                switch ($favcolor) {
                    case "REFUND":
                    case "STALE":
                        if(date("d-m-Y",strtotime($funding_dt)) >= date("d-m-Y",strtotime('31-01-2007')))
                        {
                            $this->db->where('id', $id);
                            $this->db->update($this->db_sdip, array(
                                'remarks1 '     => 'Rejected',
                                'amc_remark'    => 'Check in FD'
                            ));  
                        }
    
                        if( (date("d-m-Y",strtotime($funding_dt)) >= date("d-m-Y",strtotime('01-05-2003'))) and (date("d-m-Y",strtotime($funding_dt)) <= date("d-m-Y",strtotime('31-01-2007'))))
                        {
                           if($remarks1 = '')
                           {
                                $this->db->where('id', $id);
                                $this->db->update($this->db_sdip, array(
                                    'remarks1 '     => 'Approved',
                                    'amc_remark'    => 'Office Note Dated '.date('d-m-Y')
                                ));  
                           }else{
                                $this->db->where('id', $id);
                                $this->db->update($this->db_sdip, array(
                                    'remarks1 '     => 'Rejected',
                                    'amc_remark'    => 'Wrong Funding Date'
                                ));  
                           }
                        }
                      break;              
                    default:
                      echo "Invalid Input";
                  }
         
            }
    
           
        }
        else
        {
            return NULL;
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果您正在阅读本文并且年份是 2021 年及以后,并且您还使用 PHP > 8.0,那么您现在可以为此使用新的 ma​​tch 表达式。

      这可能是

      $var3 = match($var2){
          1, 2    => 'Weekly',
          3       => 'Monthly',
          4, 5    => 'Quarterly',
          default => 'Annually',
      };
      

      请注意,match 会进行身份检查,这与 === 相同,而开关相等性检查是 ==

      阅读更多关于匹配表达式here

      【讨论】:

        【解决方案3】:

        您可以使用数组来存储匹配组;喜欢:

            <?php
            $names = array('Ian', 'Jack', 'Fred', 'Ismail');
            $name = 'Vladimir';
            switch ($name) {
            case (in_array($name, $names)):
                    echo '<p> Welcome ' . $name . '</p>';
                    break;
                default:
                    echo '<p>' . $name . ' is a stranger to me?</p>';
            }
        ?>
        

        【讨论】:

          【解决方案4】:

          Switch 对于A/B testing 也非常方便。下面是随机测试四个不同版本的代码:

          $abctest = mt_rand(1, 1000);
          switch ($abctest) {
              case ($abctest < 250):
                  echo "A code here";
                  break;
              case ($abctest < 500):
                  echo "B code here";
                  break;
              case ($abctest < 750):
                  echo "C code here";
                  break;
              default:
                  echo "D code here";
                  break;
          

          【讨论】:

          • 恭喜您获得第一个答案!但它是脱机的:)
          【解决方案5】:

          就性能而言,最简单也可能是最好的方法是:

          switch ($var2) {
              case 1:
              case 2:
                 $var3 = 'Weekly';
                 break;
              case 3:
                 $var3 = 'Monthly';
                 break;
              case 4:
              case 5:
                 $var3 = 'Quarterly';
                 break;
          }
          

          另外,也可以用于更复杂的情况:

          switch ($var2) {
              case ($var2 == 1 || $var2 == 2):
                  $var3 = 'Weekly';
                  break;
              case 3:
                  $var3 = 'Monthly';
                  break;
              case ($var2 == 4 || $var2 == 5):
                  $var3 = 'Quarterly';
                  break;
          }
          

          在这种情况下,$var2 必须设置,不能为 null 或 0

          【讨论】:

          • ($var2 == 1 || $var2 == 2) 可能无法正常工作,具体取决于 $var2 是什么。如果$var2 = 0 将执行第一个案例。它也比普通的case 风格更冗长。你也经常breaking
          • @deceze,同意,这就是为什么我把另一个解决方案放在第一位,它只是“复杂”布尔比较的一个例子
          • 如果你要使用这种风格,至少通过使条件正常工作来正确地做到这一点:switch (true) { case ($a || $b) : ... }
          • @deceze 如果要这样做,那么case 3 将不再起作用,但我会加注
          【解决方案6】:
          switch ($var2) {
                 case 1 :
                 case 2 :
                    $var3 = 'Weekly';
                    break;
                 case 3 :
                    $var3 = 'Monthly';
                    break;
                 case 4 :
                 case 5 :
                    $var3 = 'Quarterly';
                    break;
          }
          

          第一个匹配 case 之后的所有内容都将被执行,直到找到 break 语句。所以它只是落入下一个案例,它允许您“分组”案例。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-11-26
            • 2010-09-09
            • 2013-05-15
            • 2014-10-31
            • 1970-01-01
            • 2020-04-04
            相关资源
            最近更新 更多