我知道这是简单的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')." →</a>";
}
我究竟做错了什么?
发布于 2018-08-31 09:13:37
我不确定你想要什么,但是这个逻辑总会评估为true
。您可能想要使用AND(&&),而不是OR(||)
经过测试的最远的语句是($str != "danielle"
),只有两个可能的结果,因为一旦语句产生true,PHP就会进入块。
这是第一个:
$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')." →</a>";
}
发布于 2018-08-31 10:18:13
另一种方法是
$name = 'dan';
$names = array('joe', 'danielle', 'heather', 'laurie', 'dan');
if(in_array($name,$names)){
//the magic
}
https://stackoverflow.com/questions/-100002512
复制相似问题