Array instead of Switch

From: Date: Sat, 22 Feb 2003 03:28:22 +0000
Subject: Array instead of Switch
Groups: php.general 
Request: Send a blank email to php-general+get-136587@lists.php.net to get a copy of this message
Let's say I need to take one of 20 actions depending on a form selection. I could use a switch statement with 20 cases, but I could also do something like: // Pretend this comes from a form $formchoice = "mars"; $response = array( "mars" => "go_mars()", "mercury" => "go_mercury()", "earth" => "go_earth()"); foreach ($response as $choice => $action) { if (strtoupper($formchoice) == strtoupper($choice)) { eval("$action;"); } } But are there even better ways?

« previous php.general (#136587) next »