Connecting Tech Pros Worldwide Help | Site Map

Switch question

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 01:09 AM
Jon
Guest
 
Posts: n/a
Default Switch question

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, 01:09 AM
Andy Hassall
Guest
 
Posts: n/a
Default 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, 01:11 AM
Terence
Guest
 
Posts: n/a
Default 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, 01:11 AM
Disco Plumber
Guest
 
Posts: n/a
Default 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, 01:12 AM
Chris Haynes
Guest
 
Posts: n/a
Default 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.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

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 220,989 network members.