PHP: Multiple conditions with NOT Equal (!=) operator is not working [duplicate]

那年仲夏 提交于 2020-01-04 17:52:25

问题


Hope u people will be fine. I’m working on a following code in which i want to use multiple php if conditions with multiple not operators (example is below), but when i execute following php code, it always returns true (mean content in always parenthesis always executed) even no condition is true. I want to ask what is the problem in following code. Is there any specific syntax or rule for using multiple != operator in php conditions. And i am amazed to see that if i use following code by replacing != operator with == operator it is working fine.

if( $ext!="exe" || $ext!="html" ||  $ext!="htm"  ||  $ext!="js" ||  $ext!="iso" ||  $ext!="zip"  ||  $ext!="rar" )
{ // ececk extension
    echo $ext."extension";

}
else{
    echo "not match";   
}

Waiting for your kind replies. and sorry for my bad english.


回答1:


Better code:

$allowed = array('jpeg','png');

if(in_array($ext,$allowed)){
  echo "Correct";
}
else {
 echo "Wrong";
}



回答2:


User XOR operator instead of OR as or checked all condition need to be identical while XOR return true if anyone from all is identical:

  if(!($ext!=="exe") xor ($ext=="html")){ //Add more conditions
    //each condition in new brackets & ! sign in start before all conditions

 { // ececk extension
                echo $ext."extension";

            }
            else{
                echo "not match";   
            }

Hope it will help



来源:https://stackoverflow.com/questions/16249373/php-multiple-conditions-with-not-equal-operator-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!