Grant Martin
Grant Martin

Reputation: 135

PHP if/elseif logic with && and ||

I have some php code for running a different mySQL queries based on an value sent from some HTML. The value sent from the HTML is the current day which I got using:

<?php $day = date("w"); ?>

When I use:

<?php echo $day; ?>

it returns 6, which is what it is supposed to do (because it is Saturday), so I can confirm that my HTML/php is sending the proper information to my script.

Now in my script I have a big list of mySQL queries in if/elseif statments with both && and || conditions.

Here is an example of the first two ones:

if(($noshow == 1) && ($day != 5 || $day != 6 || $day != 0)){
$run = mysql_query("UPDATE employee_data SET points=points+5 WHERE ID=" . $id) or die(mysql_error());
}
elseif(($noshow == 1) && ($day == 5 || $day == 6 || $day == 0)){
$run = mysql_query("UPDATE employee_data SET points=points+7 WHERE ID=" . $id) or die(mysql_error());
}

The variable $noshow is sent as well from my html file where it is in the form of a check box with a value of 1 (if checked). So what I need it to do is to run a query based on what day it is. In the first statement I want it to update points to equal the current points plus 5, if the box in the html is checked, and if it is not Friday, Saturday, or Sunday (5,6,0).

The next statement is supposed to do the same thing as the first, but only if the day is Friday, Saturday, or Sunday. The script goes on with about 10 other statements for various check boxes/options that I have made.

So the problem I am having, is that it is only running the first statement, even with the day being 6 (which I explained above)

I have been going in circles for about two hours with this problem and it is driving my crazy. My current thought is that my syntax for the if statement isn't correct.

Upvotes: 3

Views: 23613

Answers (2)

Hanky Panky
Hanky Panky

Reputation: 46900

Very Simple. Your first statement will work if day = 6 because you said

IF DAY IS NOT EQUAL TO 5
OR
DAY IS NOT EQUAL TO 6

So when day is not equal to 5, it will work ! And your second statement has elseif so it will not work when first did. If you want your first statement not to work when day = 6 then you have to tell it

IF DAY IS NOT EQUAL TO 5
AND
DAY IS NOT EQUAL TO 6

So a simple update would be

if(($noshow == 1) && ($day != 5 && $day != 6 && $day != 0)){
$run = mysql_query("UPDATE employee_data SET points=points+5 WHERE ID=" . $id) or die(mysql_error());
}
elseif(($noshow == 1) && ($day == 5 || $day == 6 || $day == 0)){
$run = mysql_query("UPDATE employee_data SET points=points+7 WHERE ID=" . $id) or die(mysql_error());
}

Tip:

Whenever you are confused about how your IF condition will work, translate && and || into english and read. For example your first statement would be read as

IF NO SHOW IS 1 AND (DAY IS NOT EQUAL TO 5 OR DAY IS NOT EQUAL TO 6 OR DAY IS NOT EQUAL TO 0)

Reading it that way will make it more obvious that this statement will be true even on day 6

Upvotes: 5

joe42
joe42

Reputation: 657

If $day != 5 it will return true because you are using ||, try using && to make sure all tests are required.

Upvotes: 0

Related Questions