Michael McKinney Michael McKinney - 7 months ago 18
PHP Question

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?

Answer

That is not how you do multiple conditions.

switch( $ext) {
case "txt":
case "rtf":
case "log":
case "docx":
    $pClass = "text-";
    break;
case ...
}