开发者

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.

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!!!)

Thanks in advance.


The way you tried it does not work because the comparison operator == is a binary operator and expects two operands, i.e. operand1 == operand2. The same applies to the logical OR operator that is also a binary operator, i.e. 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)). 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(strtolower(substr($file, -3)), array("mp4","mov","flv"))

And pathinfo is probably better to get the file name extension, so:

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. 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
}


A more concise way:

if ( preg_match('/(mp4|mov|flv)$/', $file) ) { ...


If you want to match only specific filenames in a directory, you can use 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.

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


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:

$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):

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).


$ext = strtolower(substr($file, -3));

switch ( $ext ) { case 'mp4': case 'mov': case 'flv': /// some operation break; }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜