PHP eval(array_as_string) returns null

后端 未结 4 757
独厮守ぢ
独厮守ぢ 2021-01-15 03:47
$arr = eval(\"array(\'foo\'=>\'bar\');\");

// returns null
var_dump($arr);

Can someone please explain why did I get null instead of an array?

4条回答
  •  遥遥无期
    2021-01-15 04:23

    You need to return the array.

    From the docs:

    eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned.

    So you need to do:

    $arr = eval("return array('foo'=>'bar');");
    

提交回复
热议问题