Jacob Schindler
Jacob Schindler

Reputation: 31

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.

Upvotes: 3

Views: 351

Answers (8)

Gumbo
Gumbo

Reputation: 655249

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

Upvotes: 11

thomasbabuj
thomasbabuj

Reputation: 3919

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

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

Upvotes: 0

aularon
aularon

Reputation: 11110

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

Upvotes: 0

Gordon
Gordon

Reputation: 316979

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

Upvotes: 1

Toto
Toto

Reputation: 91415

Another way to do it :

if ( in_array(strtolower(substr($file, -3))),  array('mp4', 'mov', 'flv') ) {
    // do something
}

Upvotes: 2

Stephen
Stephen

Reputation: 6087

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

Upvotes: 4

Matteo Riva
Matteo Riva

Reputation: 25060

A more concise way:

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

Upvotes: 1

Jake N
Jake N

Reputation: 10583

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

 if($ext == "mp4" || $ext == "mov" || $ext == "flv"){...

Upvotes: 2

Related Questions