Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP check value against multiple values with OR-operator

I have a filename($fname) and I need to assign $pClass to the file type with a "-" afterwards. Currently I always get text-, no matter what file type it is.

//This gets the extention for the file and assigns the class to the icon <i>
$pieces = explode('.', $fname);
$ext = array_pop($pieces);

if($ext == (('txt')||('rtf')||('log')||('docx'))){
  $pClass = 'text-';
}
else if($ext == (('zip')||('sitx')||('7z')||('rar')||('gz'))){
  $pClass = 'archive-';
}
else if($ext == (('php')||('css')||('html')||('c')||('cs')||('java')||('js')||('xml')||('htm')||('asp'))){
  $pClass = 'code-';
}
else if($ext == (('png')||('bmp')||('dds')||('gif')||('jpg')||('psd')||('pspimage')||('tga')||('svg'))){
  $pClass = 'image-';
}
else {
  $pClass = '';
}

Why doesn't my if statement with the OR operator works?

like image 965
mcky Avatar asked May 01 '16 20:05

mcky


People also ask

What is === in php?

=== Operator: This operator is used to check the given values and its data type are equal or not. If yes, then it returns true, otherwise it returns false.

How compare two variables in if condition in PHP?

The comparison operator called Equal Operator is the double equal sign “==”. This operator accepts two inputs to compare and returns true value if both of the values are same (It compares only value of variable, not data types) and return a false value if both of the values are not same.

How do you check if a variable is not equal to multiple values?

To check if a variable is not equal to multiple values: Use the logical and (&&) operator to chain multiple conditions. In each condition, use the strict inequality operator (! ==) to check that the variable is not equal to the value. If all conditions pass, the variable is not equal to any of the values.


3 Answers

The logical ||(OR) operator doesn't work as you expect it to work. The || operator always evaluates to a boolean either TRUE or FALSE. So in your example your strings get converted into booleans and then compared.

If statement:

if($ext == ('txt' || 'rtf'|| 'log' || 'docx'))

Comes down to:

if($ext == (TRUE || TRUE || TRUE || TRUE))
if($ext == TRUE)

To solve this problem and get the code to work as you want it to you can use different methods.

Multiple comparison

One way to solve the problem and check your values against multiple values is, to actually compare the value against multiple values:

if($ext == "txt" || $ext == "rtf" /* || ... */)

in_array()

Another way is to use the function in_array() and check if the value is equal to one of the array values:

if(in_array($ext, ["txt", "rtf" /* , ... */], TRUE))

Note: Second parameter is for strict comparison

switch()

You could also use switch to check your value against multiple values and just let the case fall through.

switch($ext){

    case "txt":
    case "rtf":
 /* case ...: */
        $pClass = "text-";
    break;

}
like image 77
Niet the Dark Absol Avatar answered Oct 07 '22 21:10

Niet the Dark Absol


I would simply change it to something like this:

//This gets the extention for the file and assigns the class to the icon <i>
$pieces = explode('.', $fname);
$ext = array_pop($pieces);
if(in_array($ext,array('txt','rtf','log','docx'))){
    $pClass = 'text-';
}elseif(in_array($ext,array('zip','sitx','7z','rar','gz'))){
    $pClass = 'archive-';
}elseif(in_array($ext,array('php','css','html','c','cs','java','js','xml','htm','asp'))) {
    $pClass = 'code-';
}elseif(in_array($ext,array('png','bmp','dds','gif','jpg','psd','pspimage','tga','svg'))){
    $pClass = 'image-';
}else {
    $pClass = '';
}
like image 21
NatWeb Solutions Avatar answered Oct 07 '22 23:10

NatWeb Solutions


You can use in_array() to compare a value to multiple strings:

if(in_array($ext, array('txt','rtf','log','docx')){
    // Value is found.
}
like image 35
Chaim Avatar answered Oct 07 '22 22:10

Chaim