<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><title><![CDATA[Rob Thompson]]></title><description><![CDATA[Comments]]></description><link>http://rob.sun3.org/</link><copyright><![CDATA[Copyright Rob Thompson]]></copyright><generator>sNews CMS</generator><item><title><![CDATA[Switch vs. If - Shane]]></title><description><![CDATA[I'm having a hard time wrapping my mind around the logic behind "switch (true)".  Can someone help me understand why that works?    
  
Thanks!  
  
Shane]]></description><pubDate>Tue, 26 Feb 2008 18:59:36 +0000</pubDate><link>http://rob.sun3.org/php-code/switch-vs-if/#Comment7</link><guid>http://rob.sun3.org/php-code/switch-vs-if/#Comment7</guid></item><item><title><![CDATA[Switch vs. If - Robert]]></title><description><![CDATA[what bugs me about switch statements is the lack of curly bracket syntax for the individual cases, which i find easier to read at a glance:  
  
switch($a)  
{  
   case(10)  
   {  
     echo("omg");  
   }  
   case(20)  
   {  
     echo("wtf");  
   }  
   case(30)  
   {  
     echo("bbq?");  
   }  
}]]></description><pubDate>Sun, 03 Feb 2008 06:14:45 +0000</pubDate><link>http://rob.sun3.org/php-code/switch-vs-if/#Comment6</link><guid>http://rob.sun3.org/php-code/switch-vs-if/#Comment6</guid></item><item><title><![CDATA[Switch vs. If - Josh Johnston]]></title><description><![CDATA[Micro optimizations are great and all, but they are no substitute for optimizing the real bottlenecks: Databases and File I/O]]></description><pubDate>Fri, 01 Feb 2008 20:30:50 +0000</pubDate><link>http://rob.sun3.org/php-code/switch-vs-if/#Comment5</link><guid>http://rob.sun3.org/php-code/switch-vs-if/#Comment5</guid></item><item><title><![CDATA[Switch vs. If - Unomi]]></title><description><![CDATA[I've done a lot of benchmarking several things lately. One of these things is the switch against the if statement.  
  
I came to the same conclusion and therefore use the switch statement a lot more nowadays. It is easier to read too.  
  
It is also worth to mention that putting your most obvious match on top of the list (like default:) and then the second most obvious match etc. This makes the switch exit as soon as possible leaving the other matches not needed anymore.  
  
Rewriting your scripts on the right spots can be a huge win if you take these things in mind. Even if you make complex switch cases, it is really a win.  
  
Another topic is the foreach against a for loop etc.  
  
I never use the foreach loop actually, only if it is clearer in my mind. I rather do this with an array:  
  
$blah = array(0 =>  'blah', 1 =>  'blah');  
$blah_keys = array_keys($blah);  
$blah_vals = array_values($blah);  
$blah_cnt = count($blah);  
  
for ($i = 0; $i  < $blah_cnt; $i++) {  
  
  $key = $blah_keys[$i];  
  $val = $blah_vals[$i];  
}  
  
One would think it is much more code. True. Most of the time you only need $blah_vals anyway. But it is so much faster in most cases.  
  
You can also do other funny benchmarking. Like finding out if defining a constant really helps and if it doesn't clugs the memory. Or finding out how fast variable variables are or re-asigning variables with different sizes and types. So much to find out...  
  
- Unomi -]]></description><pubDate>Fri, 01 Feb 2008 16:58:47 +0000</pubDate><link>http://rob.sun3.org/php-code/switch-vs-if/#Comment4</link><guid>http://rob.sun3.org/php-code/switch-vs-if/#Comment4</guid></item><item><title><![CDATA[Switch vs. If - ApeHanger]]></title><description><![CDATA[>  case($a = 0):  
  
should be case ($a == 0):  
otherwise $a is 0 from this assignment.  
  
Better way to write the comparisons is  
  
case (0 == $a):  
  
so the interpreter would warn you, if you try to assign (0 = $a) a value to a constant.  
  
ApeHanger]]></description><pubDate>Tue, 29 Jan 2008 04:01:51 +0000</pubDate><link>http://rob.sun3.org/php-code/switch-vs-if/#Comment3</link><guid>http://rob.sun3.org/php-code/switch-vs-if/#Comment3</guid></item></channel></rss>