define() an array?
Posted: Wed May 24, 2006 1:12 pm
Is this possible...
...somehow? Doesn't work as is. FOO[1] gives unexpected [.
Code: Select all
define('FOO',array(1,2,3));A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
define('FOO',array(1,2,3));Another way, I like this one betterYou can't use arrays in constants, so I came up with this two line function to get around it.
<?php
function const_array($constant) {
$array = explode(",",$constant);
return $array;
};
?>
So now if you define a constant like
<? define('myconstant','item1,item2,item3') ?>
and then use my function
<? $myarray = const_array(myconstant); ?>
$myarray will now contain an array with
item1
item2
item3
Code: Select all
/*Constants MUST evaluate to scalar values only.
You are encouraged to use serialize/unserlialize
to store/retrieve an array in a constant:*/
define('CONST',serialize(array('a','b','foo'=>'bar')));
var_dump(CONST);
var_dump(unserialize(CONST));