PHP switch case 在 case 中存在多个值

发布于 2024-10-01 13:35:17 字数 658 浏览 1 评论 0原文

我有一个变量保存值“每周”、“每月”、“季度”和“年度”,还有另一个变量保存从 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 做到这一点?

I have a variable that holds the values 'Weekly', 'Monthly', 'Quarterly', and 'Annual', and I have another variable that holds the values from 1 to 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.
}

It isn't beautiful, because my code has a lot of duplicates. What I want:

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

How can I do it in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦途 2024-10-08 13:35:18

从性能角度来看,最简单也可能是最好的方法是:

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

The simplest and probably the best way performance-wise would be:

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

Also, possible for more complex situations:

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

In this scenario, $var2 must be set and can not be null or 0

神爱温柔 2024-10-08 13:35:18
switch ($var2) {
       case 1 :
       case 2 :
          $var3 = 'Weekly';
          break;
       case 3 :
          $var3 = 'Monthly';
          break;
       case 4 :
       case 5 :
          $var3 = 'Quarterly';
          break;
}

第一个匹配案例之后的所有内容都将被执行,直到找到break语句。因此,它只会进入下一个案例,这允许您对案例进行“分组”。

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

Everything after the first matching case will be executed until a break statement is found. So it just falls through to the next case, which allows you to "group" cases.

无言温柔 2024-10-08 13:35:18

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

请注意,匹配会

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

进行身份检查,这与 === 相同,而开关相等性检查则为 ==

此处了解有关匹配表达式的更多信息

If You're reading this and the year is 2021 and beyond, You're also using PHP > 8.0, you can now use the new match expression for this.

this could be

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

Please note that match does identity checks, this is the same as === compared to switch equality check which is ==.

read more about match expression here

漫雪独思 2024-10-08 13:35:18

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

$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;

Switch is also very handy for A/B testing. Here is the code for randomly testing four different versions of something:

$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;
伴梦长久 2024-10-08 13:35:18

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

    <?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>';
    }
?>

You could use array to store you match groups; like:

    <?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>';
    }
?>
青柠芒果 2024-10-08 13:35:18
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;
    }
}
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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文