syntax error, unexpected '=' in eval()'d code

梦想与她 提交于 2019-12-13 01:29:26

问题


What is wrong with this code:

<?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 />'; "); ?>

It's exactly one line code(dont ask why) but for readablility I repeat

<?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 />';
");  
?>

I get this error: Parse error: syntax error, unexpected '=' in ...\test2.php(1) : eval()'d code on line 1


回答1:


When using double quotes, you have to escape every dollar sign, without it, php will try to resolve variables from your scope into the final string.

Since you probably don't have $start variable defined, it is treated as empty string and your code starts with '='.




回答2:


Try this:

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 />\';
');


来源:https://stackoverflow.com/questions/17384864/syntax-error-unexpected-in-evald-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!