The error blocker removes the error message. eval and echo are not functions and cannot be called using variable functions. For example:
$ Func = 'eval'
$ Func () is invalid. It will prompt that there is no eval function. If you define such a function, it is also problematic. Because eval is a keyword.
The eval call is similar to include. If no return is specified in the included file, null is returned. If the file we need to check is directly eval, the code in the file to be checked will be executed. This is not what we want. We only need to check whether the syntax of this file is correct. We can add a return statement before the file to be checked, so that the Code jumps out in advance, then the subsequent code will not be executed. Okay, that's it. The Code is as follows:
- < ?PHP
- if(!function_exists('PHP_check_syntax')) {
- function PHP_check_syntax($file_name,
&$error_message = null) {
- $file_content = file_get_contents($file_name);
- $check_code = "return true; ?>";
- $file_content = $check_code .
$file_content . "< ?PHP ";
- if(!@eval($file_content)) {
- $error_message = "file: " .
realpath($file_name) . " have syntax error";
- return false;
- }
- return true;
- }
- }
- if(!PHP_check_syntax("file.PHP", $msg)) {
- echo $msg;
- }
- else {
- echo "Woohoo, OK!";
- }
- < ?PHP
- foreach:: a => b
- ?>
Because Parse error cannot be processed by set_error_handler. This exception cannot be caught. So @ is used to suppress errors. The problem is that we cannot get detailed error information. However, I only need to check whether the syntax is correct. If it is incorrect, re-compile the template file. As for syntax errors, you will naturally see them when displaying webpages.
The best way for PHP to check syntax errors is to return the abandoned PHP_check_syntax method to PHP. Next time I will try again to find out why they removed the function.