2013-06-29 31 views
0

什麼不對這個代碼:語法錯誤,意想不到的EVAL「=」()'d代碼

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

這正是一行代碼(不要問爲什麼),但readablility我重複

<?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

+4

爲什麼地球上,你做的一切都是在'的eval()'聲明? – Eric

+0

我有一個理由,它是更大的一部分。 – Dharman

+0

刪除'eval()',它可以正常工作:http://codepad.org/HlnP46j9 – Eric

回答

8

當使用雙引號,你必須逃脫每美元符號,如果沒有它,PHP將試圖解決從變量你的範圍進入最後的字符串。

由於您可能沒有$start變量定義,因此它被視爲空字符串,並且您的代碼以'='開頭。

+0

我知道這很簡單。 (捂臉)。問題已關閉 – Dharman

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 />\'; 
'); 
相關問題