By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,203 Members | 1,383 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,203 IT Pros & Developers. It's quick & easy.

Switch question

P: n/a
Jon
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
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sat, 15 Nov 2003 19:16:05 GMT, "Jon" <ru******@msn.com> wrote:
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....
}


switch($voteraction){
case '1':
case '2':
case '3':
$db_set .= "Rating = $voteraction, ";
break;
}

http://uk2.php.net/switch

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #2

P: n/a
Jon wrote:
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;
}


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
}

Jul 17 '05 #3

P: n/a
Andy Hassall (73.075% quality rating):

switch($voteraction){
case '1':
case '2':
case '3':
$db_set .= "Rating = $voteraction, ";
break;
}


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.
Jul 17 '05 #4

P: n/a
"Jon" <ru******@msn.com> wrote in message news:<VX*******************@news2.news.adelphia.ne t>...
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;
}


I'd use:

switch($voteraction){
case 1: case 2: case 3: case...:
$db_set .= "Rating = $voteraction, ";
break;
}

Cheers,

Chris.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.