In any situation where you have an unknown row, and you need to find out which of several rows it matches, the only solution that does not get slower when you add more elements is to use an array, but have all possible rows as keys . Therefore, your switch can be replaced by 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);
It doesn't look as clean as switch() , but it is the only quick solution that does not include writing a small library of functions and classes to keep it in order. It is still very easy to add elements to arrays.
too much php Aug 21 '09 at 2:50 2009-08-21 02:50
source share