PHP Experts, Optimization question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Brujah
    Beer Money Baron
    • Jan 2001
    • 22157

    #1

    PHP Experts, Optimization question

    Looking to optimize a lengthy switch/case statement. Working on something like this, and considering how to optimize it and speed it up. It's not that slow yet but it might be when it's finished.

    Code:
    switch($var) {
      case 'val1':
          $action = 'step1';  break;
     case 'val2':
          $action = 'step2';  break;
    
     .. and so on for about 200 more of these ..
    
     default:
          $action = 'do the hokey pokey';  break;
    }
  • Echo
    Confirmed User
    • Nov 2003
    • 158

    #2
    if your $action are text/word try to put them in array() or store them in your database and call it in a limited query output
    ICQ #228-338-185

    Comment

    • paterson3713
      Confirmed User
      • Nov 2005
      • 190

      #3
      Yeah you could do like:
      Code:
      $actions['val1'] = 'step1';
      $actions['val2'] = 'step2';
      
      // Then, 
      $action = $actions[$var];
      Or, like it was also mentioned before, using a database:
      Code:
      // First, put the values into a mysql database like: value | action
      // Then,
      $action = @mysql_result(mysql_query('SELECT action FROM mySteps WHERE value=\''.mysql_escape_string($var).'\''),0);

      Comment

      • s9ann0
        Confirmed User
        • Sep 2001
        • 4873

        #4
        fuck that get your host to install a PHP cache!

        Comment

        • mrkris
          Confirmed User
          • May 2005
          • 2737

          #5
          Originally posted by paterson3713
          Yeah you could do like:
          Code:
          $actions['val1'] = 'step1';
          $actions['val2'] = 'step2';
          
          // Then, 
          $action = $actions[$var];
          Or, like it was also mentioned before, using a database:
          Code:
          // First, put the values into a mysql database like: value | action
          // Then,
          $action = @mysql_result(mysql_query('SELECT action FROM mySteps WHERE value=\''.mysql_escape_string($var).'\''),0);
          Never mix storage with procedure. The easiest thing to do, would be something like:

          $method = 'some' . $action;
          if (method_exists(array(&$object, $method))) {
          $function->$method;
          }

          I would do something like that ...

          PHP-MySQL-Rails | ICQ: 342500546

          Comment

          Working...