note 85790 deleted from function.eval by peehaa
| From: | peehaa@php.net | Date: | Mon, 09 Dec 2013 12:27:39 +0000 |
| Subject: | note 85790 deleted from function.eval by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197881@lists.php.net to get a copy of this message | ||
Note Submitter: luke at cywh dot com
----
Finally, a good use for eval :)!
If you want to be able to check for syntax errors WITHOUT executing the code, add "return
true;" before all the code. All execution of the code stops after that mark, and makes eval
returns true on no syntax errors, and false on syntax errors.
This is especially useful for anyone making a template system.
Here's a working example:
<?php
function check_syntax($code) {
return @eval('return true;' . $code);
}
print "<b>No Code execution:</b><br />\n";
$code = "print \"<b><i>hello! you don't want
me...</i></b><br />\n\";";
var_dump(check_syntax($code)); // Good syntax
print "<br />\n";
var_dump(check_syntax($code . ' ==')); // Bad syntax
print "<br />\n";
print "<b>Code Executed...Bad</b>:<br />\n";
var_dump(eval($code) === null); // Good syntax
print "<br />\n";
var_dump(@eval($code . ' ==') === null); // Bad syntax
?>