Justtechjobs.com Find a programming school near you






Online Campus Both


php-general | 2003022

[PHP] Array instead of Switch From: Chris (chrisl_ak <email protected>)
Date: 02/21/03

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?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php