在PHP中评估字符串

问题描述:

Well. I'm having a problem with the eval(); function in PHP. I don't quite understand how to store the returned data into a variable to print.. my code is as follows:

<?php 
    $a = 4; 
    $write = eval("$a+$a;"); 
    echo $write; 
?>

I'm not sure what I'm doing wrong. When I run the PHP script, all it does is output nothing.. Any help is appreciated

嗯。 我遇到了eval()的问题; PHP中的函数。 我不太明白如何将返回的数据存储到变量中进行打印..我的代码如下: p>

 &lt;?php 
 $ a = 4;  
 $ write = eval(“$ a + $ a;”);  
 echo $ write;  
?&gt; 
  code>  pre> 
 
 

我不确定我做错了什么。 当我运行PHP脚本时,它所做的就是什么都不输出.. 非常感谢 p> div>

From the PHP documentation:

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

try eval("\$write = \$a+\$a;")

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

<?php 
    $a = 4;
    eval("\$write = \$a+\$a;");
    var_dump($write); 
?>

It will return int(8)

<?php 
    $a = 4; 
    $write = eval("return $a+$a;"); 
    echo $write; 
?>