Is this method right?
switch ($_GET['get']){
case 'show':
echo 'show';
break;
case is_numeric($_GET['app']):
echo $_GET['app'];
break;
}
Is this method right?
switch ($_GET['get']){
case 'show':
echo 'show';
break;
case is_numeric($_GET['app']):
echo $_GET['app'];
break;
}
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 (Seems to be valid, cf. my edit.)case keyword.
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.
The code is valid.
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.
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']);