Using string variables for the cases in the "switch" command

Submitted by: Clay Helberg Last Updated: 2006-07-31

The documentation for the "switch" command (help 810) states that "The case constant label allows for variable substitution". This is obviouly true for numerical values only, not for strings. Clay Helberg has found a way of working around this undocumented limitation of ACL.

$firstchoice = "One"; $secondchoice = "Two"; $thirdchoice = "Three"; $quote = '"'; $firstquote = $quote . $firstchoice . $quote; $secondquote = $quote . $secondchoice . $quote; $thirdquote = $quote . $thirdchoice . $quote; global $choices[]; split("$firstchoice|$secondchoice|$thirdchoice",$choices,"|"); $mychoice = list_response($choices); switch ($mychoice) {  case $firstquote:     response("You chose the first choice");     break;   case $secondquote:     response("You chose the second choice");     break;   case $thirdquote:     response("You chose the third choice");     break;   default:     response("I couldn't find your response '" . $mychoice ."'");     break; }
 * 1) wrap the values in quotes so they work in the switch block
 * 1) the switch value itself should *not* contain quotes,
 * 2) but the case values should