What's quicker, an array lookup (including array build) or an IF stack?
<p>I was wondering which was better:</p>

<pre><code>$lookup = array( "a" =&gt; 1, "b" =&gt; 2, "c" =&gt; 3 );
return $lookup[$key];
</code></pre>

<p>or</p>

<pre><code>if ( $key == "a" ) return 1
else if ( $key == "b" ) return 2
else if ( $key == "c" ) return 3
</code></pre>

<p>or maybe just a nice switch...</p>

<pre><code>switch($key){
case "a": return 1;
case "b": return 2;
case "c": return 3;
}
</code></pre>

<p>I always prefer the first method as I can separate the data from the code; At this scale it looks quite silly but on a larger scale with thousands of lines of lookup entries; How much longer is PHP going to take building an array and then only checking maybe 1 or 2 entries per request.</p>

<p>I think it'd have to be tested and clocked, but I'd say the bigger and more complicated the array the slower it's going to become.</p>

<p>PHP Should be able to handle lookups faster than I can in PHP-code, but building the array in the first place surely takes up a lot of time.</p>
