1

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

7
  • 4
    Why on Earth are you doing it all in an eval() statement? Commented Jun 29, 2013 at 21:29
  • I have a reason, it's part of something bigger. Commented Jun 29, 2013 at 21:30
  • 3
    There isn't really any good reason for using eval() whether it's part of something bigger or not - there's always a better way than eval() Commented Jun 29, 2013 at 21:30
  • 1
    @Dharman It being part of something bigger does not make it less unnerving. Commented Jun 29, 2013 at 21:32
  • 3
    Use single quotes around the outer string. and escape the inner quotes. You have a variable $start which PHP is interpolating in double quotes to a scalar, to which you cannot assign via =. Commented Jun 29, 2013 at 21:32

2 Answers 2

10

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

Sign up to request clarification or add additional context in comments.

1 Comment

I knew it was something simple. (facepalm). Question closed
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.