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!

2
  • I improved my answer based on a comment. Commented May 20, 2013 at 15:15
  • As long as you use switch-case for all your values, your code is also very secure, since you never use any of the user provided values directly. Commented May 20, 2013 at 19:11

4 Answers 4

11

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 But you can change switch($_GET['login']) and the like to just switch($value)
0

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.

Comments

0

Maybe this helps someone nowadays using at least PHP8. You can make the code shorter and more concise using the match expression:

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

This code uses the match expression, which was introduced in PHP 8, to simplify the nested switch statements. The match expression returns the value of the matching case directly.

Comments

-2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.