...

View Full Version : Cases vs. if/else statements



BobDoleX
10-19-2003, 03: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";
break;
case 2:
$destination = "Bad";
break;
default:
$destination = "Ok";
break;
}


instead of



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


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

raf
10-19-2003, 09: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.

firepages
10-19-2003, 12: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 ?

missing-score
10-19-2003, 08: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum