Switch multiple case statement

前端 未结 3 1732
醉酒成梦
醉酒成梦 2021-02-04 23:13

Can someone suggest me how can I replace the below code?

How do I rewrite the code in order to avoid the repetition of the block case 3:{code block A; break;}

相关标签:
3条回答
  • 2021-02-04 23:48

    This format is shown in the PHP docs:

    switch (i) {
        case 1:
        case 3:
            code block A;
            break;
        case 2:
            code block B;
            break;
        default:
            code block default;
            break;
    }
    
    0 讨论(0)
  • 2021-02-05 00:02

    Something like

        $i = 10;
        switch($i){
            case $i == 1 || $i > 3:
                echo "working";
                break;
            case 2:
                echo "i = 2";
                break;
            default: 
              echo "i = $i";
              break;
        }
    
    0 讨论(0)
  • 2021-02-05 00:08

    Something like this

    switch(i) {
        case 1:
        case 3:  {code block A; break;}
    
        case 2:  {code block b; break;}
    
        default: {code block default; break;}
    }
    
    0 讨论(0)
提交回复
热议问题