Going nuts trying to determine the case order in a switch statement

The makers of Rogaine are making a fortune off me, right now. :frowning:
I have a function that I’m creating for parsing chat commands in the chat script I’m developing. There are three different “modes” for these commands: Javascript, PHP, or both. The parsing of the commands depends on the “mode”, or “framework” of said command. I want to handle the selection of parsing methods through a switch statement, and I don’t want to double-up on the coding. To explain better, here’s the function so far:


  function parseCommands($msg) {
    global $lastFunction;
    $lastFunction .= "_parseCommands";
    $commandList = getCommands();
    foreach ($commandList as $cmd) {
      if (false === strpos($msg, $cmd)) continue;
      $sql = "select command, framework, adminOnly from chat_commands where symbol = '$cmd';";
      $cmdData = getDB($sql);
      $command = $cmdData['command'];
      $framework = $cmdData['framework'];
      $adminOnly = $cmdData['adminOnly'];
      switch ($framework) {
        case "js":
          // parse command for javascript here
          break;
        case "php":
          // parse command for PHP here
          break;
        case "both":
          // parse command for javascript and php combined here here
          break;
        default:
          // you should never get here
      }
    }
    return $msg;
  }

In the instance above, you have a “doubling up” of code between the different sections. I’ve thought of just creating separate functions to parse the commands (e.g. parseJs() and parsePHP()), and just calling the seperate functions within the switch statement, but I’m hoping to find an even more efficient way to do this. Is this the best I can hope for? Or is there a better way?

You mean the third ‘both’ bit? What about just if($f = ‘js’ || $f = 'both)

I’m assuming you’re referring to replacing the criteria for the case labeled “both” with something like:

case (("both") or ("js")):

Or, perhaps:

case (($framework == "js") or ($framework == "both")):

Because, if you are, there seems a flaw there, since the break statement in the “js” case will not allow “js” conditions to pass further. Or maybe I’m missing something?

Rather replace the switch with two ifs.

Why do you want to use a switch?

Because, to me, a switch statement is more readable than if/elseif/else statements. And if I just have the switch statement populated with function calls, then code bloat is more manageable, I think. That’s actually the way I’m leaning, at this point, TBH.

I guess it’s personal taste, but that shouldn’t over ride functionality, surely two ifs will be better in this case?

switch ($framework) {
    case "js":
        do_js_stuff();
        break;
    case "php":
        do_php_stuff();
        break;
    case "both":
        do_js_stuff();
        do_php_stuff();
        break;
}

if('js' == $framework || 'both' == $framework) {
    do_js_stuff();
}
if('php' == $framework || 'both' == $framework) {
    do_php_stuff();   
}

Thanks, hash. I’ll go that way. In this instance, your code looks a bit leaner, and it’s decidedly readable enough. :slight_smile: