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

前端 未结 6 1461
暗喜
暗喜 2021-01-06 10:22

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

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


        
6条回答
  •  一个人的身影
    2021-01-06 10:28

    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
    

提交回复
热议问题