Implementing a simple lookup table with PHP -


i find myself needing simple php lookup table don't want bother using database.

for instance, might have:

1 stands "good" 2 stands "bad" 3 stands "ugly" 

two ways implemented shown below. 1 more efficient other? there other more intuitive ways implement this?

switch($code) {     case 1:$result="good";break;     case 2:$result="bad";break;     case 3:$result="ugly";break;     default:$result=null; }  $array=array(1=>"good",2=>"bad",3=>"ugly"); $result=$array[$code]; 

it's matter of going lookup.

  • if it's lookup of key -> value pairs - array way go
  • if want perform different actions based on key - it's use case strategy pattern - no case or array way @ all.

so, case option inferior in cases, less scalable , not able change in run time.

to simulate default case, use like

$result = in_array($key, $lookup) ? $lookup[$key] : $default; 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -