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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to program an "OR" condition?

Status
Not open for further replies.

james0816

Programmer
Jan 9, 2003
295
US
What is the best way to do this? I have a list of 10 conditions and currently looking for three of them.

if $a=1, 2 or 3

thx
 
If you're checking the same variable for different values, use a switch statement with the possible values you're looking for. That will save you a LOT of typing of if.... else if statements.

Lee
 
but to answer your specific question
Code:
$a=3;
if ($a === 3 || $a === 2 || $a === 1){
 //do something
}
an alternative might be
Code:
$test1 = array (1, 2, 3);
$test2 = array (4, 5, 6);
if (in_array($a, $test1)) {
 //do something
elseif (in_array($a, $test2)) {
 // do something else
} else {
 //whatever
}

as trollacious said, using switch statements can be neat too

Code:
$a=2;
switch ($a){
  case 1:
  case 2:
  case 3:
   //do something
   break;
  case 4:
  case 5:
  case 6:
   //do something else
   break;
  default:
   //whatever
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top