On Mon, 2010-11-15 at 16:27 -0500, Steve Staples wrote:
> Ok, dumb question, and i have tested, but I want to ensure that my tests
> were accurate, and behavior is correct.
>
> Ok, i have an integer, that based on what it is, does certain things...
>
> switch ((int)$intval)
> {
> }
>
> now, if i need to do something on a few of the numbers, then:
> case 0:
> # do nothing as strings will be (int) as 0
> break;
> case 1:
> # do this
> break;
> case 12:
> # do this
> break;
> default:
> # do everything that is general to this
>
> which this works, as the $intval should never be anything that i am not
> expecting as there is another part of the code that decides which int's
> to send to this class.
>
> but is there a better way to do this? this could potentially do a lot
> more, and be more dynamic, but there is also the possibilty that i need
> to have more "custom" commands based on an integer value, and not
> something that is "generic".
>
> Is there a way to only do from like cases 2-11, without doing:
> case 2:
> case 3:
> case 4:
> .....
> case 11:
> # do this stuff
> break;
>
>
> am I just overthinking things? i dunno... it's monday... brains still
> on weekend mode.
>
> Steve
>
>
There is actually a very cool way you can use a switch in PHP to do what
you want:
switch(true)
{
case ($intval >= 2 && $intval <= 11):
{
// stuff here for cases 2-11 inclusive
break;
}
}
Although I think in your case a series of if/else if statements would
work, and might possibly be more readable in your code.
Thanks,
Ash
http://www.ashleysheridan.co.uk