Connecting Tech Pros Worldwide Help | Site Map

Switch question

  #1  
Old July 17th, 2005, 02:09 AM
Jon
Guest
 
Posts: n/a
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


  #2  
Old July 17th, 2005, 02:09 AM
Andy Hassall
Guest
 
Posts: n/a

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)
  #3  
Old July 17th, 2005, 02:11 AM
Terence
Guest
 
Posts: n/a

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
}

  #4  
Old July 17th, 2005, 02:11 AM
Disco Plumber
Guest
 
Posts: n/a

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.
  #5  
Old July 17th, 2005, 02:12 AM
Chris Haynes
Guest
 
Posts: n/a

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.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
thread context switch question William Stacey answers 13 November 15th, 2005 12:51 PM
Switch Question Craig S answers 1 November 15th, 2005 06:51 AM