note 85790 added to function.eval
| From: | luke at cywh dot com | Date: | Wed, 17 Sep 2008 20:12:31 +0000 |
| Subject: | note 85790 added to function.eval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-144496@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 209.41.74.194
Probable Submitter: 74.93.181.202
----
Manual Page -- http://www.php.net/manual/en/function.eval.php
Edit -- https://master.php.net/note/edit/85790
Del: integrated -- https://master.php.net/note/delete/85790/integrated
Del: useless -- https://master.php.net/note/delete/85790/useless
Del: bad code -- https://master.php.net/note/delete/85790/bad+code
Del: spam -- https://master.php.net/note/delete/85790/spam
Del: non-english -- https://master.php.net/note/delete/85790/non-english
Del: in docs -- https://master.php.net/note/delete/85790/in+docs
Del: other reasons-- https://master.php.net/note/delete/85790
Reject -- https://master.php.net/note/reject/85790
Search -- https://master.php.net/manage/user-notes.php