Switch question 
July 17th, 2005, 02:09 AM
| | |
In some languages, you can do the following in a Switch statement...is there
a way to do this in PHP? Thanks in advance
switch($voteraction){
case '1','2','3','4','5','6','7','8','9','10':
$db_set .= "Rating = $voteraction, ";
break;
}
or do I have to the following:
switch($voteraction){
case '1':
$db_set .= "Rating = $voteraction, ";
break;
case '2':
$db_set .= "Rating = $voteraction, ";
break;
case '3':
$db_set .= "Rating = $voteraction, ";
break;
etc....
}
--
*********************
Jon Rosenberg www.DeanForAmerica.com www.OhioForDean.org | 
July 17th, 2005, 02:09 AM
| | | | re: Switch question
On Sat, 15 Nov 2003 19:16:05 GMT, "Jon" <ruffles_@msn.com> wrote:
[color=blue]
>In some languages, you can do the following in a Switch statement...is there
>a way to do this in PHP? Thanks in advance
>
>switch($voteraction){
>case '1','2','3','4','5','6','7','8','9','10':
> $db_set .= "Rating = $voteraction, ";
> break;
>}
>
>or do I have to the following:
>switch($voteraction){
>case '1':
> $db_set .= "Rating = $voteraction, ";
> break;
>case '2':
> $db_set .= "Rating = $voteraction, ";
> break;
>case '3':
> $db_set .= "Rating = $voteraction, ";
> break;
>etc....
>}[/color]
switch($voteraction){
case '1':
case '2':
case '3':
$db_set .= "Rating = $voteraction, ";
break;
} http://uk2.php.net/switch
--
Andy Hassall (andy@andyh.co.uk) icq(5747695) ( http://www.andyh.co.uk)
Space: disk usage analysis tool ( http://www.andyhsoftware.co.uk/space) | 
July 17th, 2005, 02:11 AM
| | | | re: Switch question
Jon wrote:
[color=blue]
> In some languages, you can do the following in a Switch statement...is there
> a way to do this in PHP? Thanks in advance
>
> switch($voteraction){
> case '1','2','3','4','5','6','7','8','9','10':
> $db_set .= "Rating = $voteraction, ";
> break;
> }
>[/color]
an alternative could be
if(in_array($voteraction,array('1','2','3','4','5' ,'6','7','8','9','10'))) {
// whatever
}
elseif(...)
another alternative could be a regular expression match but my knowledge
is very shakey in that area. Perhaps something like the following
(someone with regex knowledge would have to confirm this).
if (preg_match("/^\d.$/", $voteraction)) {
// whatever
} | 
July 17th, 2005, 02:11 AM
| | | | re: Switch question
Andy Hassall (73.075% quality rating):[color=blue]
>
> switch($voteraction){
> case '1':
> case '2':
> case '3':
> $db_set .= "Rating = $voteraction, ";
> break;
> }[/color]
If you have to do a huge number of cases, a somewhat less classy-looking
way would be (assuming voteraction is an int):
switch(true) {
case (1<=$voteraction && $voteraction<=10):
$db_set .= "Rating = $voteraction, ";
break;
...
If it's not an int, you could cast it to one first, use floor(), or whatever.
Of course, if you're doing the above, you're probably better off with
if-else blocks.
/joe
--
Andy James practically disrespects the phatcave. In Krispy Kreme, the
triple-poorly-executed emo kid is uberordinary. | 
July 17th, 2005, 02:12 AM
| | | | re: Switch question
"Jon" <ruffles_@msn.com> wrote in message news:<VXutb.1361$_i1.849053@news2.news.adelphia.ne t>...
[color=blue]
> In some languages, you can do the following in a Switch statement...is there
> a way to do this in PHP? Thanks in advance
>
> switch($voteraction){
> case '1','2','3','4','5','6','7','8','9','10':
> $db_set .= "Rating = $voteraction, ";
> break;
> }[/color]
I'd use:
switch($voteraction){
case 1: case 2: case 3: case...:
$db_set .= "Rating = $voteraction, ";
break;
}
Cheers,
Chris. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|