8

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

Minishlink
  • 140
  • 6

9 Answers9

22

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

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

Adam Kiss
  • 11,811
  • 9
  • 48
  • 81
  • 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 Jan 24 '10 at 16:07
  • 1
    @Minishlink: Both is probably O(n) although parsing the regular expression and building the DFA takes time too. – Gumbo Jan 24 '10 at 16:18
  • 1
    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 Jan 24 '10 at 16:21
  • 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.0680808544s 2) regex (0.076596021652222+0.063315153121948+0.071804046630859+0.074262142181396+0.074447154998779)/5 = 0.0720849038s 1<2 : in_array _W0N_ ! So in_array is better in my case. – Minishlink Jan 24 '10 at 16:51
  • Good to know, congratulations! :-) – Heinzi Jan 24 '10 at 18:14
  • 1
    @Minishlink Good, although 5 iterations is hardly a benchmark ;) – Gabriele Petrioli Jan 25 '10 at 20:22
12

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
  • 15,244
  • 5
  • 33
  • 45
4

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
  • 34,957
  • 11
  • 60
  • 92
  • 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 Jan 24 '10 at 16:02
  • 1
    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 Jan 24 '10 at 19:19
  • 1
    @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. –  Jan 25 '10 at 17:23
3

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

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
3
if(($ext == 'zip') || ($ext == 'png')) { echo "It's possible." }
Matteo Tassinari
  • 18,121
  • 8
  • 60
  • 81
Laizer
  • 5,932
  • 7
  • 46
  • 73
  • I've also found this solution, but when I have a lot of extensions it's pretty long to write. Thank you anyway ! :) – Minishlink Jan 24 '10 at 15:59
2

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
  • 12,414
  • 18
  • 67
  • 93
2

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 PHP and Enumerations 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.

Community
  • 1
  • 1
1
if (in_array($ext, array('png', 'zip'))) {
    echo "Is it possible ?"
}

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

whiskeysierra
  • 5,030
  • 1
  • 29
  • 40
0

You can use simply :

if($ext == 'zip' || $ext == 'png') 
{ 
   echo "It's possible";
}
Javed Khan
  • 395
  • 5
  • 9