Jump to content

Pros and Cons of using SWITCH


Atrix

Recommended Posts

Hi,I was just wondering if there were any cons to using the Switch function over if..elseif.. statements.I know the pro of switch is so that you dont have to have long lines of if...elseif.. statements, but no tutorial tells you if there are any cons to using switch over if..elseifs..or when the best time to use if...elseif and switch.I just want to learn the best practices of when you should use switch and when you shouldn't. That's if there any cons, there might not be and it might be just what ever it suits the coder.

Link to comment
Share on other sites

If you have complex conditions, you probably won't be able to express them efficiently in a switch statement.

Link to comment
Share on other sites

If you need to test more than one value, for example. Likeif ( (isset($_POST['user']) && $age > 37) || $pizza == 'cheese' )hard to handle that with a switch.As far as I'm concerned, switch is mostly syntactic candy for the programmer -- a way of avoiding the difficulty of reading multiple if-else blocks. As soon as you start nesting if-blocks in a case statement, all that disappears.FWIW, you see no significant performance differences until you hit thousands of iterations. IMO: unless you're really HAMMERING on a database, use the structure that works best and looks easiest to read and maintain.

Link to comment
Share on other sites

Also if you need to test more than one condition for a variable:

if ($a > 5 && $a < 20)

Basically, any condition involving a logical operator.

Link to comment
Share on other sites

FWIW, you see no significant performance differences until you hit thousands of iterations. IMO: unless you're really HAMMERING on a database, use the structure that works best and looks easiest to read and maintain.
When you say that. Do you mean if your hammering the database if..elseif statements slow it down and switch would be quicker or the other way around?Just want to make sure I understand the effect it has.
Link to comment
Share on other sites

Do you mean if your hammering the database if..elseif statements slow it down
No, hammering the database is going to slow it down, not a PHP control structure.
Just want to make sure I understand the effect it has.
Test it out. You can set up a loop to run a piece of code several thousand times and use microtime to keep track with how long it took.
Basically, any condition involving a logical operator.
Not necessarily:
switch (true){  case $a < $b:  break;  case $b < $c:  break;    case $c < $d:  break;}

And if you think that looks weird, you'll love Duff's Device:

send(to, from, count)register short *to, *from;register count;{	register n=(count+7)/8;	switch(count%8){	case 0:	do{	*to = *from++;	case 7:		*to = *from++;	case 6:		*to = *from++;	case 5:		*to = *from++;	case 4:		*to = *from++;	case 3:		*to = *from++;	case 2:		*to = *from++;	case 1:		*to = *from++;		}while(--n>0);	}}

Yeah, he interleaved a switch and a loop.

Link to comment
Share on other sites

Not necessarily:
switch (true) {  case $a < $b:	break;  case $b < $c:	break;  case $c < $d:	break;}

So that's how that's done! I always wondered if that was possible with PHP. I've tried to do it but I could never get it to work.
And if you think that looks weird, you'll love Duff's Device:
send(to, from, count)register short *to, *from;register count;{	register n=(count+7)/8;	switch(count%8){	case 0:	do{	*to = *from++;	case 7:		*to = *from++;	case 6:		*to = *from++;	case 5:		*to = *from++;	case 4:		*to = *from++;	case 3:		*to = *from++;	case 2:		*to = *from++;	case 1:		*to = *from++;		}while(--n>0);	}}

Yeah, he interleaved a switch and a loop.

That's insane.
Link to comment
Share on other sites

Not necessarily:
Well, when I said logical operators I meant only &&, ||, !, etc, and not comparison operators.
Link to comment
Share on other sites

The operators don't matter, just the values the expressions evaluate to. A switch statement is just a series of expressions which all get compared to a certain value, and the first one which matches gets executed.

switch (true){  case $a || $b:  break;    case $c || $d:  break;    case $e && $f:  break;  case !$g:  break;}

Link to comment
Share on other sites

Reading about Duff's Device, someone said that it functions incorrectly in the event of len/8 having no modulus, which I thought myself from reading it. But, it doesn't - if there's no remainder, then length is exactly 8, so you need to do the operation completely. If there is a remainder, then you need to deal with the remainder first, right? It's one of those bits of code that looks like magic and, at least for me, requires some serious thought. I'm glad that I don't have to deal with memory allocation :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...