Re: [PHP] switch vs elseif
- From: dmagick@xxxxxxxxx (Chris)
- Date: Tue, 13 Jan 2009 08:57:45 +1100
tedd wrote:
At 3:32 PM -0500 1/12/09, Robert Cummings wrote:One has to wonder about the readability of the case version though since
one may not notice immediately the missing break statement.
Cheers,
Rob.
Yes, but that's because of the way you wrote it -- consider this:
I'm sure that was completely intentional.
<?php
switch( $foo )
{
case 0:
// something
break;
case 2:
case 3:
// something else
// something elser
break;
default:
// something defaulty
}
?>
If you group the case's together, then there's no problem understanding what happens in case 2 or 3.
What happens if (like in Rob's example) I want a bunch of extra stuff to happen for case 2 but not 3?
You can either group them like you have and have:
case 2:
case 3:
if ($foo == 2) {
// do extra stuff
}
....
break;
or not put them under each other:
case 2:
// do extra stuff
case 3:
....
break;
--
Postgresql & php tutorials
http://www.designmagick.com/
.
- References:
- switch vs elseif
- From: "Frank Stanovcak"
- Re: [PHP] switch vs elseif
- From: Robert Cummings
- Re: [PHP] switch vs elseif
- From: tedd
- switch vs elseif
- Prev by Date: Re: [PHP] switch vs elseif
- Next by Date: Re: [PHP] Couple of beginner questions
- Previous by thread: Re: [PHP] switch vs elseif
- Next by thread: Re: [PHP] switch vs elseif
- Index(es):