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

前端 未结 6 1415
暗喜
暗喜 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:48

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

    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 reads as "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.

提交回复
热议问题