Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you do a comparison in a case/switch?

Status
Not open for further replies.

geokor

Programmer
Aug 16, 2000
126
US
What I want to accomplish is this:

switch ($id)
{
case 1:
blah;
break;
case 2:
blah blah;
break;
case (($id > 3)&&($id < 9)):
blah blah blah;
break;
default:
sigh;
break;
}

So if $id = 1 then blah code is executed, if $id=2 then blah blah, and if $id is 4,5,6,7,8 then blah blah blah, otherwise sigh. (In my real situation the parameters are 5 and 2000 so I can't list them all seperately.) I can't seem to figure out how to get that two stage comparison. I've tried
case ($id >3) && ($id < 9)
case (9 > $id < 3)
but nothing works. Is this complex comparison possible in php case switch? Do I need to use a series of else ifs?

Thanks.

George K

 
Do this:
if(($asd >3) && ($asd < 9)){
$id=3;
} else{
$id=$asd;
}

switch ($id)
{
case 1:
blah;
break;
case 2:
blah blah;
break;
case 3:
blah blah blah;
break;
default:
sigh;
break;
}

Then you pass in a value of $asd instead of $id.

Rick
If I have helped you just click the first link below to let me know :)
 
ristmo2001, are you a genius or what? Your solution is great! Simple, to the point. Don't why I didn't think of it! Thanks a 1,000,000!

George K
 
Sure :-D

Rick If I have helped you just click the first link below to let me know :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top