简体   繁体   English

我可以在php中的开关内使用if语句吗?

[英]Can I use an if statement inside of a switch in php?

Heres what I want to do: 这是我想做的:

I have a variable named $user, where the information is being POSTED from a form. 我有一个名为$ user的变量,其中的信息是从表单中发布的。 If the form data is empty I want to do one thing, if the form data is just a comma or symbol I want to do another thing, and if the form data is correct I want to do a third thing. 如果表单数据为空,我想做一件事,如果表单数据只是逗号或符号,我想做另一件事,如果表单数据是正确的,我想做第三件事。 This is how I have it set up, will this work? 这就是我的设置方式,这行得通吗? If not, what can I do to make it work? 如果没有,我该怎么做才能使其正常工作?

switch($user){
    case 1:
if(empty($user)){
echo "<h2>"."<strong>"."You entered NOTHING, PLEASE Re-Enter"."</strong>"."</h2>";
}
case 2:
if($user == ",") {
echo "<h2>"."<strong>"."You did not enter any valid names/abbreviations to search, PLEASE Re-Enter"."</strong>"."</h2>";
}
default:


    print "==========================================="."<br />";
    tester($company, $user);


print "==========================================="."<br />";
print "========The following were NOT found====="."<br />";
notFound($company, $user);

print "==========================================="."<br />";

}

Right now it is automatically going to default. 现在,它会自动变为默认值。 It runs, but I want integrate the two qualifiers, how would I go about doing that? 它可以运行,但是我想整合两个限定符,我将如何去做呢?

You need to put break at the end of each case in a switch statement or it falls through to the next case. 您需要在switch语句中的每个案例的末尾放置break ,否则它会进入下一个案例。 I also think you may be confused about the way switch works. 我也认为您可能对switch工作方式感到困惑。 If you're switching on $user, case matches against the value of $user--so you don't even need if-statements. 如果打开$ user,则case与$ user的值匹配-因此,您甚至不需要if语句。

switch($user){
    case "":
        echo "<h2>"."<strong>"."You entered NOTHING, PLEASE Re-Enter"."</strong>"."</h2>";
        break;
    case ",":
        echo "<h2>"."<strong>"."You did not enter any valid names/abbreviations to search, PLEASE Re-Enter"."</strong>"."</h2>";
        break;
    default:
        print "==========================================="."<br />";
        tester($company, $user);


        print "==========================================="."<br />";
        print "========The following were NOT found====="."<br />";
        notFound($company, $user);

        print "==========================================="."<br />";
}

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

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