ERuiz Posted February 27, 2007 Share Posted February 27, 2007 I am having a little problem with the following code: if ($juniorofficerexam == "no" AND $currenthours <= 49) { echo "You are eligible for the Junior Officer Exam"; } else { echo "You passed the Junior Officer Exam"; } In my case, $juniorofficerexam = no and $currenthours = 59 The result being displayed is "You passed the Junior Officer Exam". But, it should be "You are eligible for the Junior Officer Exam", because even though I have more than 49 as currenthours, I do meet the "no" criteria. How can I make this code give a true result when both criteria are NOT met. I thought the AND would do the trick. Regards, ERuiz Link to comment https://forums.phpfreaks.com/topic/40286-if-and-statement/ Share on other sites More sharing options...
bobleny Posted February 27, 2007 Share Posted February 27, 2007 I think you should try this: echo $juniorofficerexam . " - " . $currenthours; die(); if ($juniorofficerexam == "no" AND $currenthours <= 49) { echo "You are eligible for the Junior Officer Exam"; } else { echo "You passed the Junior Officer Exam"; } Link to comment https://forums.phpfreaks.com/topic/40286-if-and-statement/#findComment-194897 Share on other sites More sharing options...
ERuiz Posted February 27, 2007 Author Share Posted February 27, 2007 Thanks for your help buddy, but I kept messing around with it and this did the trick: if ($juniorofficerexam == "yes" AND $currenthours >= 50) { echo "You passed the Junior Officer Exam"; } elseif ($juniorofficerexam == "yes") { echo "You passed the Junior Officer Exam"; } else { echo "You are eligible for the Junior Officer Exam"; } Thanks! Link to comment https://forums.phpfreaks.com/topic/40286-if-and-statement/#findComment-194901 Share on other sites More sharing options...
bobleny Posted February 27, 2007 Share Posted February 27, 2007 Ahh... according to your if statement, $currenthours is illrelevant! The way you have your if statement, you pass the exam whether you $current hours is 5000 or 5.... Link to comment https://forums.phpfreaks.com/topic/40286-if-and-statement/#findComment-194903 Share on other sites More sharing options...
bobleny Posted February 27, 2007 Share Posted February 27, 2007 I don't think you have it the way your thinking. Try this: $currenthours = "20"; $juniorofficerexam = "yes"; if ($juniorofficerexam == "yes" AND $currenthours >= "50") { echo "You passed the Junior Officer Exam"; } elseif ($juniorofficerexam == "yes") { echo "You passed the Junior Officer Exam"; } else { echo "You are eligible for the Junior Officer Exam"; } Even though, $currenthours is less than 50, you will still pass the exam.... Link to comment https://forums.phpfreaks.com/topic/40286-if-and-statement/#findComment-194905 Share on other sites More sharing options...
magic2goodil Posted February 27, 2007 Share Posted February 27, 2007 One might think you should have a previous function that asks whether the hours are enough to take the exam in the first place, which in turn allows you to just ask whether or not they have a yes or not upon completing it. Also AND in C and PHP should be &&, and also OR is || which is right above the \ key. AND = && OR = || Link to comment https://forums.phpfreaks.com/topic/40286-if-and-statement/#findComment-194949 Share on other sites More sharing options...
corbin Posted February 27, 2007 Share Posted February 27, 2007 You can use AND and it will do the same thing, but it operates slightly differently. If I run across the article I'll post it because I actually thought it was a good read. Link to comment https://forums.phpfreaks.com/topic/40286-if-and-statement/#findComment-194952 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.