PHP switch case $_GET's variables and switch case $_GET's variable's values

旧巷老猫 提交于 2019-12-06 04:06:21

You have to enclose the switch in a foreach() loop.

foreach ($_GET as $key => $value) {
    switch ($key) {
        case 'login' :
            switch ($value) {
                case '0' :
                    echo 'Login failed!';
                    break;
                case '1' :
                    echo 'Login successful.';
                    break;
            }
            break;
        case 'register' :
            switch ($value) {
                case '0' :
                    echo 'Registration failed!';
                    break;
                case '1' :
                    echo 'Thank you for registering.';
                    break;
            }
            break;
        default :
            echo 'Some other message';
            break;
    }
}

I do not think it will work like this, $_get will return an array and these comparisons will not work. Switch statements need to evaluate to a constant.

Just Use this:

switch($_GET['key']) //it will return you value of particular parameter.
case 'value1':
//write your statement here.
break;
case 'value2':
//write your statement here.
break;
//and so on
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!