dougang5993 2015-02-25 20:58
浏览 63
已采纳

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

Heres what I want to do:

I have a variable named $user, where the information is being POSTED from a form. 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?

展开全部

  • 写回答

1条回答 默认 最新

  • douba5540 2015-02-25 21:13
    关注

    You need to put break at the end of each case in a switch statement or it falls through to the next case. I also think you may be confused about the way switch works. If you're switching on $user, case matches against the value of $user--so you don't even need if-statements.

    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 />";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部