Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 638
André Casal Avatar asked Sep 05 '26 00:09

André Casal


1 Answers

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;
    }
}
like image 50
Prof. Falken Avatar answered Sep 07 '26 13:09

Prof. Falken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!