2

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.

2
  • 1
    switch statements don't work that way. Commented Apr 13, 2014 at 20:43
  • @Outsider its works, just he needs few corrections! Commented Apr 13, 2014 at 20:56

3 Answers 3

2

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

1 Comment

This is such a bad idea. See my answer below for an edit where I compare this to the great evils of the day.
0

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 :)

Comments

0

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.

3 Comments

See my answer and test it! stackoverflow.com/a/23048309/2777820, the manual says that you can include expression or code! The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.
In your example, you did a mistake and you claimed it on switch! if you use if..else if its same, I don't see any thing on our main point, switch can work like if and if..else if the for the first one you can just remove the break to test all cases, please revise your logic again, I just wanna to help you nothing else.
expressions that evaluate to a boolean there is no documentation and examples to proof that, please be aware to state something that its from your conclusion, I enjoyed your post but its wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.