Jump to content

define variables (array)


Destramic

Recommended Posts

define("FILE_UPLOAD_TYPES", array('image/gif'));

i want to have this variable as a array so i can basically check when people upload if that certain file type is permitted.

[code]
if (!in_array('image/gif', FILE_UPLOAD_TYPES))
{
    echo "file not allowed";
}
[/code]

but it doesnt seem to work...are you not able to sore array's in a define varable?

thanks ricky
Link to comment
https://forums.phpfreaks.com/topic/35276-define-variables-array/
Share on other sites

constants can't be arrays, correct.

update:

figured I'd mention you can serialize them in, though, it's a bit messy

[code=php:0]
define('FILE_UPLOAD_TYPES', serialize(array('image/gif')));

if (!in_array('image/gif', unserialize(FILE_UPLOAD_TYPES)))
{
    echo "file not allowed";
}
[/code]
Could probably loop through an array using foreach and set the definition name to the ID... Such as:

foreach($array as $id => $value) {
define($id, $value);
}

But like jesirose and gennericnumber1 hinted at, using defines is incredibly messy and variables are generally better... Honestly the only time I've ever used a define is for error numbers that are used everywhere.
[quote author=genericnumber1 link=topic=123561.msg510873#msg510873 date=1169503342]
[quote author=jesirose link=topic=123561.msg510869#msg510869 date=1169503292]
Or you could just use a variable ;)
[/quote]

COULD do that, but what if he wanted to add more allowed MIME types later :P
[/quote]

Amazingly enough, one of the cool things about variables is they can vary. They can be changed. It's pretty easy to add another value to an array.
[quote author=jesirose link=topic=123561.msg510886#msg510886 date=1169503530]
Amazingly enough, one of the cool things about variables is they can vary. They can be changed. It's pretty easy to add another value to an array.
[/quote]

Do I sense a 'lil hostility :P? Fact of the matter is, multiple options are available. Whether it's constants, variables, arrays, etc... serializing constants can be messy though, good luck!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.