Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP eval(array_as_string) returns null

Tags:

php

eval

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

// returns null
var_dump($arr);

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

like image 347
user1643156 Avatar asked Mar 09 '26 14:03

user1643156


1 Answers

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');");
like image 85
Rocket Hazmat Avatar answered Mar 11 '26 02:03

Rocket Hazmat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!