gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:51:16 24 4
gpt4 key购买 nike

我知道这是简单的 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>";
}

我做错了什么?

最佳答案

我不确定您想要什么,但该逻辑的计算结果始终为 true。您可能想使用 AND (&&) 而不是 OR (||)

测试过的最远的语句是 ($str != "danielle"),只有两种可能的结果,因为 PHP 在语句为真时立即进入 block 。

这是第一个:

$str = "dan";

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

这是第二个:

$str = "joe";

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

如果 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

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

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

关于PHP if not equal(!=) and or (||) 问题。为什么这不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6311040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com