> >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?
You know you can use a variable as the name of a function?? So, if you had the functions mars(), mercury() and venus(), and you had $formchoice = 'mars'; You can just call $formchoice(); and it'll be just like calling mars(). It works the same for the other functions. You'll still want to validate it so you know it's calling on of the functions you want called. A simple array with valid planets and a call to in_array will solve that. ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php