PHP if not equal(!=) and or (||) 问题。为什么这不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6311040/
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 23:56:50  来源:igfitidea点击:

PHP if not equal(!=) and or (||) issue. Why doesnt this work?

php

提问by Greg Thompson

I know this is simple PHP logic but it just won't work...

我知道这是简单的 PHP 逻辑,但它不起作用......

 $str = "dan";
 if(($str != "joe") 
   || ($str != "danielle")
   || ($str != "heather")
   || ($str != "laurie")
   || ($str != "dan")){         

 echo "<a href='/about/".$str.".php'>Get to know ".get_the_author_meta('first_name')." &rarr;</a>";
                  }

What am I doing wrong?

我究竟做错了什么?

回答by Gazler

I am not exactly sure what you want, but that logic will always evaluate to true. You might want to use AND (&&), instead of OR (||)

我不确定您想要什么,但该逻辑将始终评估为true. 您可能想要使用 AND (&&),而不是 OR (||)

The furthest statement that is ever tested is ($str != "danielle") and there are only two possible outcomes as PHP enters the block as soon as a statement yields true.

测试过的最远的语句是 ( $str != "danielle") 并且只有两种可能的结果,因为一旦语句产生真,PHP 就会进入块。

This is the first:

这是第一个:

$str = "dan";

$str != "joe" # true - enter block
$str != "danielle" #ignored
$str != "heather" #ignored
$str != "laurie" #ignored
$str != "dan" #ignored

This is the second:

这是第二个:

$str = "joe";

$str != "joe" # false - continue evaluating
$str != "danielle" # true - enter block
$str != "heather" #ignored
$str != "laurie" #ignored
$str != "dan" #ignored

If the OR was changed to AND then it keeps evaluating until a false is returned:

如果 OR 更改为 AND,则它会继续评估,直到返回 false:

$str = "dan";

$str != "joe" # true - keep evaluating
$str != "danielle" # true - keep evaluating
$str != "heather"  # true - keep evaluating
$str != "laurie" # true - keep evaluating
$str != "dan"  # false - do not enter block

The solution doesn't scale well though, you should keep an array of the exclude list and check against that do:

但是,该解决方案不能很好地扩展,您应该保留一个排除列表的数组并检查它:

$str = "dan";
$exclude_list = array("joe","danielle","heather","laurie","dan")
if(!in_array($str, $exclude_list)){          
    echo " <a href='/about/".$str.".php'>Get to know ".get_the_author_meta('first_name')." &rarr;</a>";
}

回答by joakimdahlstrom

Another approach is

另一种方法是

$name = 'dan';
$names = array('joe', 'danielle', 'heather', 'laurie', 'dan');

if(in_array($name,$names)){  
    //the magic
}

回答by Marc B

Welcome to boolean logic:

欢迎使用布尔逻辑:

$str = 'dan'

$str != "joe" -> TRUE, dan is not joe
$str != "danielle" -> TRUE, danielle is not dan
$str != "heather") -> TRUE, heather is not dan
$str != "laurie" -> TRUE, laurie is not dan
$str != "dan" -> FALSE, dan is dan

Boolean logic truth tables look like this:

布尔逻辑真值表如下所示:

and:

和:

TRUE && TRUE -> TRUE
TRUE && FALSE -> FALSE
FALSE && FALSE -> FALSE
FALSE && TRUE -> FALSE

or:

或者:

TRUE || TRUE -> TRUE
TRUE || FALSE -> TRUE
FALSE || TRUE -> TRUE
FALSE || FALSE -> FALSE

Your statement boiled down to:

你的陈述归结为:

TRUE || TRUE || TRUE || TRUE || FALSE -> TRUE

回答by trutheality

Based on your comment in Glazer's answer, it looks like you want to enter the if block when $stris not one of the listed names.

根据您在 Glazer 的回答中的评论,您似乎想在$str不是列出的名称之一时输入 if 块。

In that case it would be more readable if you write it as

在这种情况下,如果您将其写为

if( !( ($str == "joe") || ($str == "danielle") || ($str == "heather") || ($str == "laurie") || ($str == "dan") ) )

This actually readsas "if it's not one of these people..." to someone looking at your code. Which is equivalent to the slightly less obvious

对于查看您的代码的人来说,这实际上作“如果它不是这些人中的一个……”。这相当于稍微不那么明显的

if( ($str != "joe") && ($str != "danielle") && ($str != "heather") && ($str != "laurie") && ($str != "dan") )

The fact that they're equivalent is called DeMorgan's law in logic.

它们等价的事实在逻辑上被称为德摩根定律。

回答by Patrick R

try this

尝试这个

$str = "dan";

if($str == "joe" || $str == "daniella" || $str == "heather" || $str == "laurine" || $str == "dan"){ ... }

回答by Patrick R

When you compare two strings you have to use strcmp().

当您比较两个字符串时,您必须使用 strcmp()。