Best way to do a PHP switch with multiple values per case?
How would you do this PHP switch statement?
Also note that these are much smaller versions, the 1 I need to create will have a lot more values added to it.
Version 1:
switch ($p) {
case 'home':
case '':
$current_home = 'current';
break;
case 'users.online':
case 'users.location':
case 'users.featured':
case 'users.new':
case 'users.browse':
case 'users.search':
case 'users.staff':
$current_users = 'current';
break;
case 'forum':
$current_forum = 'current';
break;
}
Version 2:
switch ($p) {
case 'home':
$current_home = 'current';
break;
case 'users.online' || 'users.location' || 'users.featured' || 'users.browse' || 'users.search' || 'users.staff':
$current_users = 'current';
break;
case 'forum':
$current_forum = 'current';
break;
}
UPDATE - Test Results
I ran some speed test on 10,000 iterations,
Time1: 0.0199389457703 // If statements
Time2: 0.0389049446106 //switch statements
Time3: 0.106977939606 // Arrays
Solution 1:
For any situation where you have an unknown string and you need to figure out which of a bunch of other strings it matches up to, the only solution which doesn't get slower as you add more items is to use an array, but have all the possible strings as keys. So your switch can be replaced with the following:
// used for $current_home = 'current';
$group1 = array(
'home' => True,
);
// used for $current_users = 'current';
$group2 = array(
'users.online' => True,
'users.location' => True,
'users.featured' => True,
'users.new' => True,
'users.browse' => True,
'users.search' => True,
'users.staff' => True,
);
// used for $current_forum = 'current';
$group3 = array(
'forum' => True,
);
if(isset($group1[$p]))
$current_home = 'current';
else if(isset($group2[$p]))
$current_users = 'current';
else if(isset($group3[$p]))
$current_forum = 'current';
else
user_error("\$p is invalid", E_USER_ERROR);
This doesn't look as clean as a switch()
, but it is the only fast solution which doesn't include writing a small library of functions and classes to keep it tidy. It is still very easy to add items to the arrays.
Solution 2:
Version 2 does not work!!
case 'users.online' || 'users.location' || ...
is exactly the same as:
case True:
and that case
will be chosen for any value of $p
, unless $p
is the empty string.
||
Does not have any special meaning inside a case
statement, you are not comparing $p
to each of those strings, you are just checking to see if it's not False
.
Solution 3:
Put those many values into an array and query the array, as the switch-case seems to hide the underlying semantics of what you're trying to achieve when a string variable is used as the condition, making it harder to read and understand, e.g.:
$current_home = null;
$current_users = null;
$current_forum = null;
$lotsOfStrings = array('users.online', 'users.location', 'users.featured', 'users.new');
if(empty($p)) {
$current_home = 'current';
}
if(in_array($p,$lotsOfStrings)) {
$current_users = 'current';
}
if(0 === strcmp('forum',$p)) {
$current_forum = 'current';
}
Solution 4:
For the sake of completeness, I'll point out that the broken "Version 2" logic can be replaced with a switch statement that works, and also make use of arrays for both speed and clarity, like so:
// used for $current_home = 'current';
$home_group = array(
'home' => True,
);
// used for $current_users = 'current';
$user_group = array(
'users.online' => True,
'users.location' => True,
'users.featured' => True,
'users.new' => True,
'users.browse' => True,
'users.search' => True,
'users.staff' => True,
);
// used for $current_forum = 'current';
$forum_group = array(
'forum' => True,
);
switch (true) {
case isset($home_group[$p]):
$current_home = 'current';
break;
case isset($user_group[$p]):
$current_users = 'current';
break;
case isset($forum_group[$p]):
$current_forum = 'current';
break;
default:
user_error("\$p is invalid", E_USER_ERROR);
}