PHP eval ()
' + ', ' name ' = ' Kuke ', ' time ' = ' ' 1024x768 ', $res = "1+3"; Var_dump ($res); $v =eval ($res); Var_dump ($v); Echo $v; foreach ($v as $key = + $val) {echo $key. = ". $val;} -------------------------------------------echo "
";//--------------------------------------------$res =array (' price ' = ' + ', ' name ' = ' Kuke ', ' time ' = ') ' Var_dump ($res); echo "
"; foreach ($res as $key = + $val) {echo $key." = ". $val."
";}
This is the result.
String (3) "1+3"
Parse error:syntax error, unexpected $end in D:\myweb\array.php (5): eval () ' D Code on line 1
BOOL (FALSE)
Warning:invalid argument supplied for foreach () in D:\myweb\array.php on line 8
Array (3) {["Price"]=> string (3) "["] ["Name"]=> string (4) "Kuke" ["Time"]=> string (4) "1024"}
price=400
Name=kuke
time=1024
Who can help me analyze, why wrong?
Reply to discussion (solution)
$v = eval ("return $res;");
Eval () returns NULL unless a value is returned in the executed code, and the function returns the value passed to return. If there is a parse error in the code that executes, eval () returns FALSE, and then the code executes normally. Parsing errors in eval () cannot be captured using Set_error_handler ().
Definition and usage
The eval () function calculates the string according to the PHP code.
The string must be a valid PHP code and must end with a semicolon.
If no return statement is called in the code string, NULL is returned. If there is a parsing error in the code, the eval () function returns FALSE.
So write this: $v =eval ("return $res;");
Warning:invalid argument supplied for foreach () in D:\myweb\array.php on line 8
This error is because foreach can only traverse arrays and objects, and you pass in $V is a bool value, so you get an error.
1.
$res = "1+3";
Eval ("\ $v = \" $res \ ";");
Var_dump ($v);
2.
$V No data