3

Say I have an URL like www.mysite.com/index.php?login=0. Is it possible to switch case $_GET's variables and switch case $_GET's variable's values?

Something like:

switch ($_GET) {
    case 'login' :
        switch($_GET['login']) {
            case '0' :
                echo 'Login failed!';
                break;
            case '1' :
                echo 'Login successful.';
                break;
        }
        break;
    case 'register' :
        switch ($_GET['register']) {
            case '0' :
                echo 'Registration failed!';
                break;
            case '1' :
                echo 'Thank you for registering.';
                break;
        }
        break;
    default :
        echo 'Some other message';
        break;
}

I'm not sure if switch case can be used on associative arrays. What am I doing wrong? Cheers!

4

3 回答 3

11

您必须将开关包含在 foreach() 循环中。

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;
    }
}
于 2013-05-20T13:19:45.720 回答
0

我不认为它会像这样工作,$_get 将返回一个数组,这些比较将不起作用。Switch 语句需要计算为常量。

于 2013-05-20T13:23:42.333 回答
-2

只需使用这个:

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
于 2013-05-20T13:31:55.593 回答