【发布时间】:2016-05-01 20:53:21
【问题描述】:
我有一个文件名($fname),之后我需要用“-”将$pClass 分配给文件类型。目前我总是得到text-,不管它是什么文件类型。
//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 = '';
}
为什么我的带有 OR 运算符的 if 语句不起作用?
【问题讨论】:
-
首先通过检查 $ext 的值被设置为什么来调试
-
我刚刚重新检查并为每个文件正确设置了 $ext(example.php 返回“php”)
-
(('txt')||('rtf')||('log')||('docx')) 将始终评估为 1,然后才与 $ext
-
您最初的
if将始终返回 true,因此您总是返回test-。 -
解决方案不属于问题。为此,我们有答案。
标签: php if-statement comparison