View Full Version : Cases vs. if/else statements

10-19-2003, 04:40 AM
i asked a question about this kinda a while ago, but now i'm wondering if using cases, ie:

switch($information) {
case 1:
$destination = "Good";
case 2:
$destination = "Bad";
$destination = "Ok";

instead of

if ($information == 1) {$destination = "Good"}
elseif ($information == 2) {$destination = "Bad"}

I'm currently using the if/elseif way, but i have around 200 or those so i think it's making the site load very slow, so i'm wondering if using the cases way would make it faster? Thanks

10-19-2003, 10:48 AM
when i check for a serie of values of 1 variable (like in your case), i always use switch (with break for each option).
i use If-elsif etc for real conditional checks on values of more then 1 variable.

the manual on switch() says something similar http://be.php.net/manual/en/control-structures.switch.php

I suppose it is faster but never tested it.

10-19-2003, 01:07 PM
lol , I do the opposite , less than 3 options I use if/else , any more I use switch , but thats just a coding preference for me rather than anything else , I dont know which is faster if any ?

10-19-2003, 09:45 PM
i do the same as you firepages, but I always use switch loops if im setting what the script would run... like in a CMS system, I switch $_GET['action'] to find out what to do.

As for speed, im not sure... never tested it.