1

Is this method right?

switch ($_GET['get']){ 
  case 'show':
        echo 'show';
  break;

  case is_numeric($_GET['app']):
        echo $_GET['app'];
  break;
}
1
  • Have you run it? To me it doesn't look like it would work, but I could be wrong. Commented Sep 13, 2010 at 12:41

4 Answers 4

4

I'm not able to tell whether this is syntactically valid but it makes no sense:

case is_numeric($_GET['app']):

You're comparing $_GET['get'] with the (Boolean) result of is_numeric(). This may be what you intend but I doubt it. Furthermore, I'm not sure if you can have expressions (i.e., non-simple language constructs) after the case keyword. (Seems to be valid, cf. my edit.)


EDIT
Based on the comments in the PHP manual it's possibly valid. And due to the loose comparison used by switch the above may actually reflect your intention. If this is the case, please ignore my answer.

Sign up to request clarification or add additional context in comments.

Comments

2

The code is valid.

1 Comment

I have NEVER seen anyone use a function after a case statement...this is a good example why people think bad things about PHP. You should try this in C#
1

I'm going to be the sarcastic old man in the corner and say I honestly don't know why anyone uses switch statements in PHP. Really. They are so horrible.

Is this what you are trying to do?

if ($_GET['get'] == "show") {
echo "show";
} else if (is_numeric($_GET['app'])) {
echo $_GET['app'];
}

That's so much simpler to understand!

The one reply to my question is to avoid calculating the same thing twice. For example, this is bad:

if (BigSlowFunctionCall() == 1) { 
... 
} else if (BigSlowFunctionCall() == 2) {
...
}

If it's 2, then you've just called BigSlowFunctionCall() twice - not efficient. But here's how you get round that:

$resultOfBigSlowFunctionCall = BigSlowFunctionCall();
if ($resultOfBigSlowFunctionCall == 1) { 
... 
} else if ($resultOfBigSlowFunctionCall == 2) {
...
}

I argue that's still much more readable than a switch statement.

Sorry. I'm not having a good day.

3 Comments

And Yes, I do know coding style is a matter of personal preference .... Sorry. Hopefully I'll cheer up soon.
Dude i really really love switch statements, Where does continue apply in a if statement? I think dismissing switch because someone misuses its purpose is just plain wrong.
@stephen I've just looked up the code examples on uk.php.net/continue and I'm still sticking to my original point of "Is that what you call readable?" Maybe there are purposes a switch statment can be used for, every time I've seen one I just think it could be replaced with an if else block and be more readable.
1

You could consider using something like this: (i would personally fetch the whole $_GET array and use a foreach loop, but there you go)

 function parseGet($getVar)
    {  
       if(is_numeric($getVar)) echo $getVar;
       else
       {
         switch($getVar)
         {
            case "show":
                echo "show";
            break;
         }       
       }        
    }

parseGet($_GET['show']);

1 Comment

What's the switch statement doing in there that a else if wouldn't?

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.