$arr = eval("array('foo'=>'bar');");
// returns null
var_dump($arr);
Can someone please explain why did I get null instead of an array?
You need to return the array.
From the docs:
eval()returnsNULLunlessreturnis called in the evaluated code, in which case the value passed toreturnis returned.
So you need to do:
$arr = eval("return array('foo'=>'bar');");
Return Value, just looked at the example which doesn't use a return.eval in the first place? It's slow and insecure. There is probably a better way to do what you want.'return $rule["value"]' . $selector . '$target;' will give me true==true but that can change depending on the variables. Is there a better way for this case?switch($selector){}, instead of using eval(). Then something like case '==': $ret = $rule['value'] == $target;.Did you mean
eval("\$arr = array('foo'=>'bar');");
var_dump($arr);
$. You are in double quotes, so PHP is trying to interpolate $arr, since it doesn't exist you wind up evaling " = array('foo'=>'bar');".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
evaling that string? There has to be a better way to do this, such asjson_decodeorunserialize.