views:

218

answers:

8

Hi all :)

I'm currently writing sort of a download manager and I was asking myself if that was possible:

if($ext == ('zip' || 'png')) { echo "Is it possible ?" }

It returns true everytime, so I guess it's not possible. But do you have an idea about how I could easily do this ? I mean, not with plenty of "if" or "switch"...

Thanks anyway ! :)

+3  A: 

You can use regular expressions, e.g.

if(preg_match("/^(zip|png)$/", $ext) { echo “It is possible!” }

Related question: Checking for file-extensions in PHP with Regular expressions

Heinzi
Oh godness, I've totally forgotten regex...Thank you !! =)
Minishlink
This seems like a poor use of regex, and a poor solution to the current problem.
Ewan Todd
+2  A: 

You could go with a switch-case statement:

switch($ext)
{ 
  case 'png':
  case 'zip':
       // Will run for both 'png' and 'zip'
       echo "It is possible";
       break;
  default:
       echo "unknown extension!";
       break;
 }
Isak Savo
Yes, but I want to do the same instructions for every extensions in $ext; so it's very long with this solution... Thank you anyway ! :)
Minishlink
I don't understand. This switch-case will do exactly what you wanted with your original code. Every time $ext contains either png or zip, "It is possible" will be output.
Isak Savo
@Minishlink It might be longer, but it's VERY easy to maintain. Don't think that short code == better code. It has it's pros and cons.
CodeMonkey
+3  A: 

if(($ext == 'zip') || ($ext == 'png')) { echo "It's possible." }

Laizer
I've also found this solution, but when I have a lot of extensions it's pretty long to write. Thank you anyway ! :)
Minishlink
+19  A: 

you could use in_array($ext,array('png','zip','another','more'))

see here: http://php.net/manual/en/function.in-array.php

Adam Kiss
It works too, but is it quicker than regex ?
Minishlink
don't no, probably depends on number of times you do it... anyhow, it was the first thing, that popped in my mind :)
Adam Kiss
@Minishlink: Both is probably O(n) although parsing the regular expression and building the DFA takes time too.
Gumbo
Another solution would be to use the extensions as a key for an index. That will be only O(1) (after initialization): `array_key_exists($ext, array_fill_keys(array('png','zip'), null))`.
Gumbo
Ok, thanks for the precisions. :)
Minishlink
I've just done some experiences, I have calculated the execution time of the page 5 times for 1° the in_array solution 2° the regex solution. Here are my results: (in seconds)1) in_array(0.073891162872314+0.064849138259888+0.074558973312378+0.064404964447021+0.062700033187866)/5 = 0.0680808544s2) regex(0.076596021652222+0.063315153121948+0.071804046630859+0.074262142181396+0.074447154998779)/5 = 0.0720849038s1<2 : in_array _W0N_ !So in_array is better in my case.
Minishlink
yay me, i'm winner! :D
Adam Kiss
Good to know, congratulations! :-)
Heinzi
@Minishlink Good, although 5 iterations is hardly a benchmark ;)
Gaby
+1  A: 
if (in_array($ext, array('png', 'zip'))) {
    echo "Is it possible ?"
}

The array could be stored somewhere, if you need it multiple times.

Willi
+7  A: 

if($ext == ('zip' || 'png')) is doing comparison in the following order -> ('zip' || 'png'), which because at least one is not null, returns TRUE. Substitute that in now, ($ext == TRUE), which I'm going to go out on a limb and guess that php is just evaluating this the same as it would ($ext), which is also true.

if ( $ext == 'zip' || $ext == 'png' ) will check what you are looking for.

mynameiscoffey
+2  A: 

It's definitely possible, but what you're doing there is incorrect code. Here is what you wrote:

if($ext == ('zip' || 'png')) { echo "Is it possible ?" }

And here is what that translates to in php:

if( (if $ext evaluates to true then return true) == ( if 'zip' evaluates to true then return true || if 'png' evaluates to true then return true ) )

So, since 'zip' isn't one of the 'empty' or 'false' values defined in php, and neither is 'png' you're basically running this if statement:

if($ext == true)

Which, if it isn't empty, it does.

What you want is - as previously mentioned:

if($ext == 'zip' || $ext == 'png')
Daniel Bingham
+2  A: 

Tiny advice: PHP uses similar to C boolean-type handling in a sense that actually any non-zero value is considered to be "true" in case of residing in conditional part of if- statement. for example if you would miss '=' symbol in compare construction and type if($var = "val") instead of if($var == "val") you will always get a true value in that statement, because '=' operator would return as a result of set-operation value from the right part "val" that is by-turn converted to 'true'. so it is better to write the literal in the left part of compare condition if("val" == $var) cause in this circumstance you would get an error if you loose one '=' symbol in '==' compare.

so your if-statement have to look like this: if('zip' == $ext || 'png' == $ext) { echo "Is it possible ?" }

also probably it would be better to put 'zip' and 'png' literals in constants with names FILE_TYPE_ZIP, FILE_TYPE_PNG or define some enumerated-entity like global PHP-array that resides at the top of your source-page or probably even create some separate class SupportedFileTypes in external file that would emphasize supported file types of your program (in that case check out http://stackoverflow.com/questions/254514/php-and-enums for details).

at the start of developing the question of performance doesn't have to bother you cause it is crucial to write code that is easy to read and evolve/optimize in future.

Serhiy Ryabokon