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
caseorarrayway @ 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
Post a Comment