【问题标题】:PHP if not equal(!=) and or (||) issue. Why doesnt this work?PHP if not equal(!=) and or (||) 问题。为什么这不起作用?
【发布时间】:2011-06-10 19:14:06
【问题描述】:

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

我做错了什么?

【问题讨论】:

  • 你希望发生什么?请描述一下。
  • 另外,您的变量设置为 dan 并且您检查变量是否不是 dan(或其他)并回显某些内容。我认为您希望看到信息,输出回显但是你的变量是dan
  • 老实说,您似乎想要== 而不是!= ...
  • 另外你可能应该在这里使用in_array()

标签: php


【解决方案1】:

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

测试过的最远的语句是 ($str != "danielle"),一旦语句为真,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')." &rarr;</a>";
}

【讨论】:

  • 只是好奇,但 && 不是意味着他们都必须是正确的吗?基本上完整的功能会得到作者的名字,并且只对上面没有提到的某些作者这样做......
  • 是的,所以如果 $str 不等于 "joe" 并且 $str 不等于 "danielle" $str 不等于 "heather" 时,您想返回 等等
  • "Right" for != 表示它不是指定作者之一。
  • @Gazler 恭喜获得逆转徽章 :-)
  • @Gazler 根据stackoverflow.com/questions/2473989/… in_array 也可以缩放 O(n)。使用数组('key' => true);和 !isset($exclude_list[$key]) 缩放 O(1) 或 O(ln(n))。无论如何,答案都很好!
【解决方案2】:

另一种方法是

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

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

【讨论】:

    【解决方案3】:

    欢迎使用布尔逻辑:

    $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
    

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

    和:

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

    或:

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

    您的陈述归结为:

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

    【讨论】:

      【解决方案4】:

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

      在这种情况下,如果你把它写成这样,它会更具可读性

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

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

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

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

      【讨论】:

        【解决方案5】:

        试试这个

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 2015-06-15
            • 1970-01-01
            • 2016-09-07
            • 2016-11-24
            • 1970-01-01
            • 2013-12-16
            • 1970-01-01
            • 2014-02-23
            • 1970-01-01
            相关资源
            最近更新 更多