Dharman
Dharman

Reputation: 33238

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

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

Upvotes: 1

Views: 16969

Answers (2)

dev-null-dweller
dev-null-dweller

Reputation: 29462

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 '='.

Upvotes: 10

Expedito
Expedito

Reputation: 7795

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

Upvotes: 0

Related Questions