Jump to content

If AND statement?


ERuiz

Recommended Posts

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

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";
}

 

;D Thanks!

Link to comment
https://forums.phpfreaks.com/topic/40286-if-and-statement/#findComment-194901
Share on other sites

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.