2

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.

Ry-
  • 209,133
  • 54
  • 439
  • 449
Haseeb
  • 2,188
  • 1
  • 19
  • 43
  • 3
    If you use `OR (||)` condition means it will check the condition one by one. If your extension `$ext = 'jpg'` means its not equal `$ext != 'exe' (condition true)`, then its omit all those remaining conditions – Ranjith Apr 27 '13 at 07:26

2 Answers2

8

Better code:

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

if(in_array($ext,$allowed)){
  echo "Correct";
}
else {
 echo "Wrong";
}
GBD
  • 15,512
  • 2
  • 42
  • 49
0

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

A.Aleem11
  • 1,710
  • 13
  • 12