或在 PHP IF 语句中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6321392/
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
OR in PHP IF Statement
提问by Damo
I am having trouble with my IF statement, it is always TRUE although this is incorrect. I'm using an OR operator as there are two possible scenarios I want to capture in the IF statement.
我的 IF 语句有问题,尽管这是不正确的,但它始终为 TRUE。我正在使用 OR 运算符,因为我想在 IF 语句中捕获两种可能的情况。
The array string ad_status is "1" but using the below -3 is returned, I'm expecting the IF to be FALSE. If I remove the OR and second statement from the IF, the result of the IF is correct.
数组字符串 ad_status 为“1”,但使用下面的 -3 返回,我期望 IF 为 FALSE。如果我从 IF 中删除 OR 和第二个语句,则 IF 的结果是正确的。
What have I done wrong? Thanks.
我做错了什么?谢谢。
if(($getadstatus['ad_status'] != "1" || $getadstatus['ad_status'] != "4"))
{
return -3;
exit;
}
Additional: What I want to do is exit the function (not seen in full here) if ad_status does not equal 1 or 4. If it equals any other value other than 1 or 4, the IF statement should return TRUE and exit. ad_status could be any value from 0 to 4.
附加:如果 ad_status 不等于 1 或 4,我想要做的是退出函数(此处未完整显示)。如果它等于 1 或 4 以外的任何其他值,则 IF 语句应返回 TRUE 并退出。ad_status 可以是 0 到 4 之间的任何值。
回答by Nanne
What you are saying is that any value that is not 1
OR is not 4
should return true.
您要说的是,not 1
OR 的任何值not 4
都应返回true。
For '1' you get the statement
对于“1”,您会得到声明
if( 1 != 1 || 1 != 4)
which translates to
这意味着
if( false || true )
which is ofcourse true.
这当然是真的。
What you need is:
你需要的是:
if(!($value == 1 || $value==4))
which is the same as (de Morgan's law)
这与(德摩根定律)相同
if($value != 1 && $value != 4)
回答by dynamic
There are no errors there.
那里没有错误。
If ad_status == 1
then your second condition will get you into the If
Ifad_status == 1
那么你的第二个条件会让你进入 If
$getadstatus['ad_status'] != "4"
is true therefore you will get return -3;
是真的,所以你会得到 return -3;
If i got what you want you should use AND
如果我得到了你想要的东西,你应该使用 AND
if ( $a!= 1 AND $a!= 4 )
回答by CristiC
You check:
你检查:
ad_status != 1 -> FALSE
ad_status != 4 -> TRUE
if (FALSE OR TRUE)
is always TRUE
.
if (FALSE OR TRUE)
总是TRUE
。
To be what you expected, replace OR with AND:
按照您的预期,将 OR 替换为 AND:
if(($getadstatus['ad_status'] != "1" && $getadstatus['ad_status'] != "4"))
{
return -3;
exit;
}
回答by Steve Mayne
It will always be true as any value can't be both '1' and '4' at the same time.
它始终为真,因为任何值都不能同时为 '1' 和 '4'。
回答by Karolis
You should use &&
operator because use !=
. If you want to use ||
you could write like this:
您应该使用&&
运算符,因为使用!=
. 如果你想使用||
你可以这样写:
if (!($getadstatus['ad_status'] == "1" || $getadstatus['ad_status'] == "4"))
if (!($getadstatus['ad_status'] == "1" || $getadstatus['ad_status'] == "4"))
回答by xylar
You want to use &&
你想使用&&
if(($getadstatus['ad_status'] != "1" && $getadstatus['ad_status'] != "4"))
{
return -3;
exit;
}
回答by Adam Moss
I personally prefer in_array instead of OR in IF statemements. Eg:
我个人更喜欢 in_array 而不是 IF 语句中的 OR 。例如:
$array = array(1,4);
if (!in_array($getadstatus['ad_status'], $array)) {
// do whatever
}
回答by hakre
humm, ok I think I see. I'm attempting to be too clever. I want to use a single IF statement to check for two non related conditions. If ad_status does not equal 1 OR 4, return -3 and exit the function.
嗯,好吧,我想我明白了。我试图变得太聪明了。我想使用单个 IF 语句来检查两个不相关的条件。如果 ad_status 不等于 1 或 4,则返回 -3 并退出函数。
Okay, no problem, that can be expressed, just formulate like you write:
好的,没问题,可以表达,就像你写的那样制定:
$status = $getadstatus['ad_status']; // assign a variable as it makes things easier to read.
if ( !( $status==1 || $status==4 ) )
{
return -3;
}
So the !
(not) should be on the whole OR comparison as you wrote in your sentence. That's probably in code what you had originally in mind. But as the order is important, the other part of your condition needs to be inside brackets to be calculated first, before using the not (!
) operator.
所以!
(not) 应该是你在句子中写的整个 OR 比较。这可能是您最初想到的代码。但由于顺序很重要,在使用 not ( !
) 运算符之前,条件的另一部分需要在括号内进行计算。
Added:
添加:
The more sub-conditions are part of a condition or expression, the more complex it gets. But the more often you formulate complex conditions the better you will get with them. To train, you can always split conditions over multiple lines and assign labels (variables) to their part:
条件或表达式的子条件越多,它就越复杂。但是,您制定复杂条件的次数越多,您对它们的处理效果就越好。要进行训练,您始终可以将条件拆分为多行并为其分配标签(变量):
$status = $getadstatus['ad_status'];
$statusIs1or4 = $status==1 || $status==4;
$statusIsNot1or4 = !$statusIs1or4;
if ($statusIsNot1or4) return -3;
For production code this might be overuse, but as it's always the authors choice how to write something, you can do whatever the language allows.
对于生产代码,这可能会被过度使用,但由于作者总是选择如何编写某些东西,因此您可以做语言允许的任何事情。