请原谅我,如果之前有这个,我搜索无济于事.
我有一个脚本,可以查看目录以查找里面的文件.有条件行只查找具有特定扩展名的文件:
if(strtolower(substr($file, -3)) == "mp4"){...
Run Code Online (Sandbox Code Playgroud)
所以这只会查找扩展名为"mp4"的文件.
我需要添加一些"或"运算符来添加两个扩展类型.我尝试了以下但它不起作用:
if(strtolower(substr($file, -3)) == "mp4" || == "mov" || == "flv"){...
Run Code Online (Sandbox Code Playgroud)
现在该行似乎被忽略,它获取目录中的每个文件.如果有人能帮助我,我将非常感激!我知道这可能是基本的,但我对PHP的掌握非常有限(尽管我看到它的美丽!)
提前致谢.
Gum*_*mbo 11
你尝试它的方式不起作用,因为比较运算符==是一个二元运算符,并期望两个操作数,即operand1 == operand2.这同样适用于也是二元运算符的逻辑OR运算符,即operand1 || operand2.
这意味着你需要写这样的东西:
$ext = strtolower(substr($file, -3));
if ($ext == "mp4" || $ext == "mov" || $ext == "flv")
Run Code Online (Sandbox Code Playgroud)
这里$ext只是用来避免重复调用strtolower(substr($file, -3)).在这种情况下,每个二元运算符都有两个操作数:
((($ext == "mp4") || ($ext == "mov")) || ($ext == "flv"))
\__/ \___/
\__==___/ \__/ \___/
\ \__==___/
\_______||_______/
\ \__/ \___/
\ \__==___/
\________________||_______/
Run Code Online (Sandbox Code Playgroud)
(我添加了括号以突出显示表达式的计算顺序.)
所以这就是你必须写它的方式.
但你也可以使用数组和in_array:
in_array(strtolower(substr($file, -3)), array("mp4","mov","flv"))
Run Code Online (Sandbox Code Playgroud)
而pathinfo可能是更好的获取文件扩展名,那么:
in_array(pathinfo($file, PATHINFO_EXTENSION), array("mp4","mov","flv"))
Run Code Online (Sandbox Code Playgroud)