语法错误,eval()'d代码中的意外'='

时间:2013-06-29 21:28:35

标签: php eval

此代码有什么问题:

<?php eval(" $start = microtime(true)*1000; echo 'hello'; $end=microtime(true)*1000; $time = $end-$start; $time = round($time,4); echo '<br />Time taken: '.$time.'ms<br />'; "); ?>

这正是一行代码(不要问为什么),但为了可读性,我重复

<?php 
eval(" $start = microtime(true)*1000; 
    echo 'hello'; 
    $end=microtime(true)*1000; 
    $time = $end-$start; 
    $time = round($time,4); 
    echo '<br />Time taken: '.$time.'ms<br />';
");  
?>

我收到此错误: Parse error: syntax error, unexpected '=' in ...\test2.php(1) : eval()'d code on line 1

2 个答案:

答案 0 :(得分:9)

当使用双引号时,你必须转义每一个美元符号,如果没有它,php将尝试将范围内的变量解析为最终字符串。

由于您可能没有定义$start变量,因此将其视为空字符串,代码以'='开头。

答案 1 :(得分:0)

试试这个:

eval(' $start = microtime(true)*1000; 
    echo \'hello\'; 
    $end=microtime(true)*1000; 
    $time = $end-$start; 
    $time = round($time,4); 
    echo \'<br />Time taken: \'.$time.\'ms<br />\';
');