How to make this work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stickman373
Forum Commoner
Posts: 30
Joined: Mon Jul 22, 2002 10:26 am

How to make this work

Post by stickman373 »

Is there a better way to do something like this and is this the correct way to do if statements and the correct way to do Or

Code: Select all

if ($f == 43 | 2 | 56 | 38) {

//Do stuff

} else {

if ($f == 34 | 53 | 4 | 9) {

//do other stuff 

} else {

if ($f == 3 | 10 |  {

//do more crap

} else {

//other things

          }  
      }
   }
}
Basically what i want it to do is if $f is 43 or 2 or 56 or 38 it does one thing but if it's 34 or 53 or 4 or 9 it does something else and if it's 3 or 10 or 8 it should do something else, but it seems to be doung the first if statement no matter what $f is.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Code: Select all

switch($f){
 case 43:
 case 2:
 case 56:
 case 38:
 echo 'its either 43,2,56, or 38';
 break;
 case 34:
 case 53:
 case 4:
 case 9:
 echo 'its either 34,53,4, or 9';
 break;
 case 3:
 case 10:
 case 8:
 echo 'its either 3,10 or 8';
 break;
 default:
 echo 'none of those.';
 }
http://www.php.net/switch ;)
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

Code: Select all

$nums = array("43","2","56","38");
$nums2 = array("34","53","4","9");

if(in_array($f,$nums)) {
  //Do stuff
} elseif(in_array($f,$nums2)) {
  //Do stuff
} else {
  //Do stuff
}
stickman373
Forum Commoner
Posts: 30
Joined: Mon Jul 22, 2002 10:26 am

Post by stickman373 »

thanks a lot dusty, that worked perfectly :D
Post Reply