Russell
Russell

Reputation: 665

PHP switch statement with $_GET variables

Pages such as this: PHP switch case $_GET's variables and switch case $_GET's variable's values and others have helped, but I am at a loss as to why my switch statment does not work.

A user may be directed to my page with index.php?class=className&badge=badgeName or index.php?class=className or index.php?badge=badgeName or just plain old index.php

This code works just fine

if ($_GET["badge"] && $_GET["class"]) {
echo 'Badge and Class'; 
} elseif ($_GET["badge"] && !$_GET["class"]) {
echo 'Badge only';   
} elseif (!$_GET["badge"] && $_GET["class"]) {
echo 'Class only';  
} else {
echo 'No variables';    
} 

But I was trying to simplify with a switch statement, whereby all works well except for the default case:

switch ($_GET) {
    case $_GET["badge"] && $_GET["class"]:
        echo 'Badge and Class';
        break;
    case $_GET["badge"] && !$_GET["class"]:
        echo 'Badge Only';
        break;
    case !$_GET["badge"] && $_GET["class"]:
        echo 'Class only';
        break;
    default:
        echo "No badge or class";
}

Any help appreciated.

Upvotes: 2

Views: 3125

Answers (3)

RobP
RobP

Reputation: 9522

I think you misunderstand the switch() statement. It is a special case of if... else if... else if for comparing a single variable to a possible set of values. You can't include conditions (expressions that evaluate to a boolean) in the case statements. See http://www.php.net/manual/en/control-structures.switch.php for examples.

EDIT: I must take a minute to rail against the abuse of the switch statement that is as follows:

$a=1;
$b=2;
switch(true) {
    case ($a==1):
        echo "first test passed";
        break;
    case ($b==2):
        echo "second test passed";
        break;
}

This compares "true" to each of the boolean results of the expressions below. Both conditions are true, the first one executes, and the break; statement skips the second one. Technically functional, wildly misleading, and I'd throttle the guy who left me this code to debug. Absolutely abominable practice. By contrast, if you switch on a value like ($a) or ($a -3) and your case statements contain comparison values like 5 and 7, or "A" and "B", a good IDE will warn you that you have duplicate case statements. You can be sure that exactly one of the case statements passes the comparison. And the people who maintain your code in future won't have to hunt you down or curse your name. Please don't use switch() this way. If you need to do it like this, use if... else if... for readability. In this way it is obvious that the tests must be checked in order and the flow of execution will be transparent.

Upvotes: 0

Jason OOO
Jason OOO

Reputation: 3552

You can try something like this:

switch (true) {
    case ($i ==0):
        echo '$i ==0';
        break;
    case ($i < 1):
        echo '$i < 1';
        break;
    case ($i > 1):
        echo '$i > 1';
        break;
}

For your case:

switch (true) {
    case ($_GET["badge"] && $_GET["class"]):
        echo 'Badge and Class';
        break;
    case ($_GET["badge"] && !$_GET["class"]):
        echo 'Badge Only';
        break;
    case (!$_GET["badge"] && $_GET["class"]):
        echo 'Class only';
        break;
    default:
        echo "No badge or class";
}

Upvotes: 2

Nicolai Kr&#252;ger
Nicolai Kr&#252;ger

Reputation: 431

As the answer are in the link you gave:

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

$_GET is simply an array, so you will have to make a for-each loop over it, as proposed in the link

foreach ($_GET as $key => $value) {
  // Switch goes here
}

Then inside this for-each you can do switch, where you use the value of $key.

How you are going to tackle this one, I haven't figured out at the moment...

[EDIT] I would stick with the if-elseif version - if you only have those two values to check for :)

Upvotes: 0

Related Questions