Pass variable to php eval ()

I am using the php function eval (), below are my instructions:

$uid = 8;
$str = 'SELECT COUNT(*) FROM uchome_blog WHERE uid=$uid';
eval("\$str = \"$str\"");
die("$str");
//$query = $_SGLOBAL['db']->query($str);
//$result = $_SGLOBAL['db']->fetch_array($query);

Conclusion: SELECT COUNT (*) FROM uchome_blog WHERE uid = $ uid This is to say that varibale $ uid did not pass. How to pass a variable to the evaluated string. Thank.

+3
source share
4 answers

you cannot directly insert variables into single-quoted strings. try the following:

$str = "SELECT COUNT(*) FROM uchome_blog WHERE uid=$uid"; // double-quotet

or that:

$str = 'SELECT COUNT(*) FROM uchome_blog WHERE uid='.$uid; // string-concatenation
+1
source

According to php manual: http://php.net/manual/en/function.eval.php

The code will be executed in the area of ​​the code that calls eval (). This way, any variables defined or changed in the call to eval () will remain visible after its completion.

, , eval(), .

+1

.

:

$uid = 8;
$str = "SELECT COUNT(*) FROM uchome_blog WHERE uid=$uid"; # variable gets substituted here
eval("\$str = \"$str\"");
die("$str");

I think variable substitution is what happens during parsing - it is not done recursively, so it is inserted into the evalcontent $strin a string, but it is not done a second time for the contents $uidinside $str.

0
source

You are missing a semicolon. Try the following:

eval("\$str = \"$str\";");
0
source

All Articles