Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有For循环PHP的直通式开关盒_Php_Switch Statement_Case_Fall Through - Fatal编程技术网

带有For循环PHP的直通式开关盒

带有For循环PHP的直通式开关盒,php,switch-statement,case,fall-through,Php,Switch Statement,Case,Fall Through,请帮忙 for($i=0; $i<12; $i++ ){ switch($i) { case 0: case 1: case 2: case 3: case 4: echo ("i is less than 5 <br>"); break; case 5:

请帮忙

for($i=0; $i<12; $i++ ){
        switch($i) {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
                echo ("i is less than 5 <br>");
                break;
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                echo ("i is less than 10 <br>");
                break;
            default:
                echo ("i is 10 or more <br>");
        }
    }

我的问题是,为什么案例0到案例3输出“i小于5”,即使它没有任何以下代码,而案例4是带有echo语句的?我很困惑,有人能给我解释一下吗。提前感谢。

这就是
开关的工作原理。为了避免陷入下一个案例,您必须使用
break
关键字。我所知道的每种语言都是一样的,包括JavaScript、PHP和Python


作为参考,请查看。

这就是“失败”所指的-如果您没有
中断在一个案例陈述之后,它将进入下一个案例。您的标题似乎表明您已经知道答案。此外,这本应该是你第一次去研究它的地方。但是为什么案例0到案例3的输出i小于5,即使它没有echo语句。我对此进行了研究,如果一个案例没有任何中断声明,它将继续进行下一个案例,直到遇到中断声明。我认为当它遇到案例4时,它应该只输出一次小于5的值
i is less than 5 
i is less than 5 
i is less than 5 
i is less than 5 
i is less than 5 
i is less than 10 
i is less than 10 
i is less than 10 
i is less than 10 
i is less than 10 
i is 10 or more 
i is 10 or more