$arr = eval("array('foo'=>'bar');");
// returns null
var_dump($arr);
Can someone please explain why did I get null instead of an array?
$arr = eval("array('foo'=>'bar');");
// returns null
var_dump($arr);
Can someone please explain why did I get null instead of an array?
Did you mean
You need to
return
the array.From the docs:
So you need to do:
The eval function executes the php code given to it. As your code returns nothing, it gives null. You need to return the array and store it in a variable like,
First of all,
eval
is highly discouraged as explained in the manual.Also, you should be doing something like
$arr = eval("return array('foo'=>'bar');");
ie. initialising$arr
with the eval function. See it in action here