简体   繁体   English

在PHP中切换案例陈述

[英]Switch case statement in PHP

I am learning PHP. 我正在学习PHP。 I have downloaded an open source project from a website and looking the workflow of each modules in that project. 我已经从网站上下载了一个开源项目,并查看了该项目中每个模块的工作流程。 I noticed a switch case which is unfamiliar to me. 我注意到一个我不熟悉的开关盒。

switch ($value) {
        case 'student':
        case StudentClass::getInstance()->getId();
            return new StudentClass();
            break;
        case 'teacher':
        case TeacherClass::getInstance()->getId();
            return new TeacherClass();
            break;
        default:
            break;
    }

The above patch is what I looked. 上面的补丁就是我的样子。 When I give input: 当我输入时:

$value = 'student';

It returns StudentClass instance. 它返回StudentClass实例。

If I give 如果我给

$value = 'teacher';

then it returns TeacherClass instance. 然后返回TeacherClass实例。

If anyone explain the flow, it will be helpful to me to understanding PHP much better 如果有人解释这个流程,对我更好地了解PHP会有所帮助

Your string cases don't have break or return statements, so they "fall through" to the next case . 您的字符串cases没有breakreturn语句,所以他们“贯穿”到下一个case Also, your break s don't serve any purpose here. 同样,您的break在这里没有任何作用。

I've added comments to your code to explain what's happening. 我已经在代码中添加了注释,以解释发生了什么。

switch ($value) {
        case 'student': // keeps going with next line
        case StudentClass::getInstance()->getId();
            return new StudentClass(); // handles both cases above
            break; // unnecessary because of the return above
        case 'teacher': // keeps going with next line
        case TeacherClass::getInstance()->getId();
            return new TeacherClass(); // handles both cases above
            break; // unnecessary because of the return above
        default:
            break; // pointless, but handles anything not already handled
}

Also, PHP explicitly allows use of a semicolon ( ; ) after a case , but it is not generally considered good style. 此外,PHP明确允许在使用case后使用分号( ; ),但通常不认为它是好的样式。 From the docs: 从文档:

It's possible to use a semicolon instead of a colon after a case... 案例后可以使用分号代替冒号...

Switch statement is used to perform different actions based on different conditions. Switch语句用于根据不同的条件执行不同的操作。

First we have a single expression n (most often a variable), that is evaluated once. 首先,我们有一个表达式n(通常是一个变量),该表达式被求值一次。 The value of the expression is then compared with the values for each case in the structure. 然后将表达式的值与结构中每种情况的值进行比较。 If there is a match, the block of code associated with that case is executed. 如果存在匹配,则执行与该情况相关的代码块。 Use break to prevent the code from running into the next case automatically. 使用break可以防止代码自动进入下一种情况。 The default statement is used if no match is found. 如果找不到匹配项,则使用默认语句。

 switch (n) { case label1: code to be executed if n=label1; break; // if n=label1,break ends execution and exit from switch case label2: code to be executed if n=label2; break; // if n=label2,break ends execution and exit from switch case label3: code to be executed if n=label3; break; // if n=label3,break ends execution and exit from switch ... default: code to be executed if n is different from all labels; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM