PHP if/elseif 逻辑与 && 和 ||

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14176141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:48:26  来源:igfitidea点击:

PHP if/elseif logic with && and ||

phpif-statement

提问by Grant Martin

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 代码,用于根据一些 HTML 发送的值运行不同的 mySQL 查询。从 HTML 发送的值是我使用的当天:

<?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.

它返回 6,这是它应该做的(因为它是星期六),所以我可以确认我的 HTML/php 正在向我的脚本发送正确的信息。

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

现在在我的脚本中,我在 if/elseif 语句中有一个很大的 mySQL 查询列表,其中包含 && 和 || 状况。

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).

变量 $noshow 也是从我的 html 文件中发送的,它以复选框的形式发送,值为 1(如果选中)。所以我需要它做的是根据它是哪一天运行查询。在第一条语句中,如果选中 html 中的框,并且不是周五、周六或周日 (5,6,0),我希望它更新点数以等于当前点数加 5。

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.

下一个语句应该与第一个语句做同样的事情,但前提是当天是星期五、星期六或星期日。该脚本继续对我所做的各种复选框/选项进行了大约 10 条其他语句。

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)

所以我遇到的问题是它只运行第一个语句,即使当天是 6(我在上面解释过)

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.

我已经围绕这个问题转了大约两个小时,这让我发疯。我目前的想法是我的 if 语句语法不正确。

回答by Hanky Panky

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

很简单。如果 day = 6,你的第一个语句会起作用,因为你说

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 elseifso 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

所以当 day 不等于 5 时,它会起作用!你的第二个语句有elseif所以它在第一次做时不起作用。如果您希望第一个语句在第 6 天时不起作用,那么您必须告诉它

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 条件的工作方式感到困惑时,请翻译 && 和 || 成英文阅读。例如,您的第一条语句将被解读为

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

以这种方式阅读它会更明显地表明即使在第 6 天这个陈述也是正确的

回答by joe42

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

如果 $day != 5 它将返回 true,因为您正在使用 ||,请尝试使用 && 以确保需要所有测试。