问题
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