A way to check PHP files for syntax errors in PHP

  • 2020-03-31 19:59:04
  • OfStack

A simple templating engine was used in a previous dangdang project, which is actually a template engine borrowed from discuz. It's very simple. All it does is compile some custom tags into PHP code. Already said is very simple, so compile the time also good for template syntax check, so in the process of development will appear compiled out of the PHP file syntax problems, there is no problem with the syntax, I modify and recompile it. Can't at the time of each request is first put to recompile PHP template, will seriously affect the performance of the compromise process at the end of each compiled PHP file to check whether the template file has been modified, according to set the update frequency, if need to recompile the template file, the question now is compiled PHP files you have syntax errors, check the step doesn't perform all templates, so even if the revised template file problem won't recompile. So I wanted to find an easy way to check if the generated PHP file was legitimate. Recompile illegally so that you don't have to manually delete the cache files during development without any errors.

I looked it up on the Internet. At first I thought the token_get_all() function would handle syntax errors, but it turned out to be a simple lexical analysis. There is no way. Later went up to the forum to ask

http://groups.google.com/group/professional-php/browse_thread/thread/b8581f6b07b10ff0/2601a63c406bb1c1? LNK = gst&q = 2601 a63c406bb1c1 reeze#

Someone told me that there is such a function php_check_syntax (http://www.php.net/manual/en/function.php-check-syntax.php) I think so resolutely.. I really should RTF(Read The Fuck Mannual). This function is almost deprecated:
Note: For technical reasons, this function is deprecated and removed from PHP. Instead, use php-l somefile.php from the commandline.

What is the technical reason? Forget it, study it slowly later, but you can't use this method.
Their recommendation is to use the command line $php-l filename.php to check the syntax.
Gary Every gave me a code snippet for reference:

Checking from the command line is not a problem either. What if I want to put it in an online app? This brings us to the issue of portability. First the operating system, then the environment variables. This will depend on the server-side configuration. At http://www.php.net/manual/en/function.php-check-syntax.php someone posted their php_check_syntax () function implementation.
Some use the above command line approach.
The eval method is mentioned later. The eval method executes the incoming code and throws a parser error if the code has syntax errors. You can use the '@' error suppressor to remove the error message.
$func = 'eval'
A call like $func() is invalid. It will tell you that there is no eval function, which is problematic if you define it yourself. Because eval is a keyword.
The eval call is similar to include in that it returns null if there is no explicit return in the included file. If we simply eval the file we need to examine, it will cause the code in the file we are examining to be executed, which is not what we want, we just need to check that the file is syntactically correct. We can add a return statement before the file we want to check, so that the code pops out ahead of time, so that the rest of the code doesn't execute. Okay, let's do it. The code is as follows:
The checker. PHP
 
<?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!"; 
} 

File. The PHP
 
<?php 
foreach:: a => b 
?> 

Because a Parse error cannot be handled by the set_error_handler handler function. This exception cannot be caught. Hence the use of @ to suppress errors. The problem with this is that we can't get detailed error information. For now, however, all I need is a syntax check. If you recompile the template file incorrectly, it's as simple as that, and as for the syntax error, you'll see it when you display the page.
The best thing to do is this abandoned php_check_syntax method back to PHP. And we'll see next time why they got rid of this function.
///////

Related articles: