seany123 Posted June 26, 2009 Share Posted June 26, 2009 does this code look right? if($player->staff != 1 || $player->staff != 4 || $player->staff != 5){ is there no way of writing it in a better way? Link to comment https://forums.phpfreaks.com/topic/163750-little-problem/ Share on other sites More sharing options...
MasterACE14 Posted June 26, 2009 Share Posted June 26, 2009 does this code look right? yep is there no way of writing it in a better way? don't think so Link to comment https://forums.phpfreaks.com/topic/163750-little-problem/#findComment-864019 Share on other sites More sharing options...
gevans Posted June 26, 2009 Share Posted June 26, 2009 you could do this; <?php $allowed_numbers = array(1, 4, 5); if(!in_array($player->staff, $allowed_numbers)) { Then if you want to add more numbers, just add to the array, rather than the conditional statement. Link to comment https://forums.phpfreaks.com/topic/163750-little-problem/#findComment-864021 Share on other sites More sharing options...
dzelenika Posted June 26, 2009 Share Posted June 26, 2009 if(!($player->staff != 1 && $player->staff != 4 && $player->staff != 5)){ De Morgan's rules (Math): !a && !b <==> !(a || b) !a || !b <==> !(a && b) Link to comment https://forums.phpfreaks.com/topic/163750-little-problem/#findComment-864029 Share on other sites More sharing options...
seany123 Posted June 26, 2009 Author Share Posted June 26, 2009 but for example with this code.. if($player->staff != 1 || $player->staff != 4 || $player->staff != 5){ header("location: home.php"); } it will redirect unless $player->staff is equal to 1, 4 or 5?? because i echo'd my $player->staff and it echo'd 5 so i shouldnt be redirected but i am. Link to comment https://forums.phpfreaks.com/topic/163750-little-problem/#findComment-864034 Share on other sites More sharing options...
gevans Posted June 26, 2009 Share Posted June 26, 2009 Your using OR. When it's set to 5, not equla to 1 and not equal to 4 return true. Change || to &&, or use my suggestion Link to comment https://forums.phpfreaks.com/topic/163750-little-problem/#findComment-864037 Share on other sites More sharing options...
Adam Posted June 26, 2009 Share Posted June 26, 2009 Could shorten it a bit, looks best way to go using an array though... if (!in_array($player->staff, array(1, 4, 5))) { Link to comment https://forums.phpfreaks.com/topic/163750-little-problem/#findComment-864039 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.