[英]PHP “or” operator within conditional statement - newb question!
Forgive me if this has been covered before, I searched to no avail. 请原谅我,如果之前有这个,我搜索无济于事。
I have a script that looks into a directory to find the files inside. 我有一个脚本,可以查看目录以查找里面的文件。 There is a conditional line that only looks for files with a certain extension:
有条件行只查找具有特定扩展名的文件:
if(strtolower(substr($file, -3)) == "mp4"){...
So that will only look for files with an 'mp4' extension. 所以这只会查找扩展名为“mp4”的文件。
I need to add some "or" operators to add two more extension types. 我需要添加一些“或”运算符来添加两个扩展类型。 I tried the following but it didn't work:
我尝试了以下但它不起作用:
if(strtolower(substr($file, -3)) == "mp4" || == "mov" || == "flv"){...
Now the line seems to be ignored and it gets every file in the directory. 现在该行似乎被忽略,它获取目录中的每个文件。 If anyone could help me out, I'd be very grateful!
如果有人能帮助我,我将非常感激! I know this is probably as basic as it gets, but my grasp of PHP is extremely limited (although I do see its beauty!!!)
我知道这可能是基本的,但我对PHP的掌握非常有限(尽管我看到它的美丽!)
Thanks in advance. 提前致谢。
The way you tried it does not work because the comparison operator ==
is a binary operator and expects two operands, ie operand1 == operand2
. 你尝试它的方式不起作用,因为比较运算符
==
是一个二元运算符,并期望两个操作数,即operand1 == operand2
。 The same applies to the logical OR operator that is also a binary operator, ie operand1 || operand2
这同样适用于也是二元运算符的逻辑OR运算符,即
operand1 || operand2
operand1 || operand2
. operand1 || operand2
。
That means you would need to write something like this: 这意味着你需要写这样的东西:
$ext = strtolower(substr($file, -3));
if ($ext == "mp4" || $ext == "mov" || $ext == "flv")
Here $ext
is just used to avoid repeated call of strtolower(substr($file, -3))
. 这里
$ext
仅用于避免重复调用strtolower(substr($file, -3))
。 In this case each binary operator has two operands: 在这种情况下,每个二元运算符都有两个操作数:
((($ext == "mp4") || ($ext == "mov")) || ($ext == "flv"))
\__/ \___/
\__==___/ \__/ \___/
\ \__==___/
\_______||_______/
\ \__/ \___/
\ \__==___/
\________________||_______/
( I added parentheses to highlight the order in which the expression is evaluated. ) ( 我添加了括号以突出显示表达式的计算顺序。 )
So this is how you would have to write it. 所以这就是你必须写它的方式。
But you could also use an array and in_array
: 但是你也可以使用数组和
in_array
:
in_array(strtolower(substr($file, -3)), array("mp4","mov","flv"))
And pathinfo
is probably better to get the file name extension, so: 而
pathinfo
可能更适合获取文件扩展名,因此:
in_array(pathinfo($file, PATHINFO_EXTENSION), array("mp4","mov","flv"))
The problem is that PHP does not automatically know that you want to compare to strtolower(substr($file, -3))
in each "or" section. 问题是PHP不会自动知道您要在每个“或”部分中与
strtolower(substr($file, -3))
进行比较。 You need to explicitly state this: 您需要明确说明:
if(strtolower(substr($file, -3)) == "mp4" || strtolower(substr($file, -3)) == "mov" || strtolower(substr($file, -3)) == "flv"){...
Note that it would probably be neater to do something like: 请注意,执行以下操作可能更为简洁:
$tmp = strtolower(substr($file, -3));
if($tmp == "mp4" || $tmp == "mov" || $tmp == "flv"){...
$ext = strtolower(substr($file, -3));
if($ext == "mp4" || $ext == "mov" || $ext == "flv"){...
Another way to do it : 另一种方法:
if ( in_array(strtolower(substr($file, -3))), array('mp4', 'mov', 'flv') ) {
// do something
}
更简洁的方式:
if ( preg_match('/(mp4|mov|flv)$/', $file) ) { ...
If you want to match only specific filenames in a directory, you can use glob
如果只想匹配目录中的特定文件名,可以使用
glob
$files = glob('/path/to/dir/*.{mp4,mov,flv}', GLOB_BRACE);
to return an array of matching file paths. 返回匹配文件路径的数组。
Or you use fnmatch
to match a filename against a pattern. 或者使用
fnmatch
将文件名与模式匹配。
In addition, if you want to make sure the images are really images, consider checking against the MimeType instead of or in addition to the extension 此外,如果您想确保图像是真正的图像,请考虑检查MimeType而不是扩展名
if you want to compare using ||
如果你想使用
||
进行比较 , here's the syntax: ,这是语法:
if(strtolower(substr($file, -3)) == "mp4" || strtolower(substr($file, -3)) == "mov" || strtolower(substr($file, -3)) == "flv"){...
of course to make it a bit faster, fill strtolower(substr($file, -3))
in a variable so php doesn't execute those functions more than once: 当然为了让它快一点,在一个变量中填充
strtolower(substr($file, -3))
,这样php就不会多次执行这些函数:
$extension=strtolower(substr($file, -3));
if($extension == "mp4" || $extension == "mov" || $extension == "flv"){...
The other good news is, PHP got a builtin function to search whether a value exists in an array of values ( in_array
): 另一个好消息是,PHP有一个内置函数来搜索值数组中是否存在值(
in_array
):
if(in_array(strtolower(substr($file, -3)), array('mp4', 'mov', 'flv')))
One last thing, if you have simple strings like those, ones who you don't want php to expand variables and stuff inside, use single qutations instead of doubles , that is a good practice and saves some execution time (of course barely noticed with a string or two, but it's a good practice on the long range). 最后一件事,如果你有简单的字符串,你不希望php扩展变量和内部的东西, 使用单个qutations而不是double ,这是一个很好的做法,节省了一些执行时间(当然几乎没有注意到一两串,但这是长距离的好习惯)。
$ext = strtolower(substr($file, -3)); $ ext = strtolower(substr($ file,-3));
switch ( $ext ) { case 'mp4': case 'mov': case 'flv': /// some operation break; switch($ ext){case'mp4':case'mov':case'flv':///某些操作中断; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.