Check PHP files for syntax errors in PHP _php tips

Source: Internet
Author: User
Tags deprecated parse error php template
Before when the time of a project used a simple template engine, in fact, is also a reference to Discuz to do the template engine, very simple, it does is to put some custom tags into PHP code. has said very simple, so the compile time is also famous for template grammar check, then in the development process will appear compiled PHP file has grammatical problems, there is no relationship between grammatical problems, I modify the compilation of the good. First of all, you can not compile the PHP template every time the request, will seriously affect performance, compromise processing in each compiled PHP file at the end to check whether the template file has been modified, according to set the frequency of updates, if necessary, recompile template files, The problem now is that the compiled PHP file has its own syntax error and does not perform the template check step at all, so even if the problem in the template file is modified it will not be recompiled. So I'm looking for an easy way to check if the generated PHP file is legitimate. Recompile is not legal, so the development process will have to manually delete the cached files without errors.

Look for it on the Internet. Just beginning to think that the Token_get_all () function can handle the problem of grammatical errors, it turns out that it is just doing a simple lexical analysis. There is no way. Then I went to the forum and asked

Http:// &q=reeze#2601a63c406bb1c1

I was told that there was such a function php_check_syntax () I think the problem is so resolute. I really should have RTF (Read the fuck mannual). Take a closer look. This function is nearly deprecated:
Note:for technical reasons, this function is deprecated and removed from PHP. Instead, use Php-l somefile.php from the commandline.

What exactly is this technical reason? Regardless of the first, and then slowly study, anyway can not use this method is right.
Their advice is to use the command line $php-l filename.php to check the syntax.
Gary every gave me a code snippet reference:

It's not a big problem to check at the command line. What if I want to put it on the online app? This involves the portability of the problem. The first is the operating system, then the environment variable. This will depend on the server-side configuration. Someone posted their php_check_syntax () function implementation on the
Some are using the command line method above.
The following is a reference to using the Eval method to validate. The Eval method executes the incoming code and throws parser error if the code has a syntax error, you can use the ' @ ' ERROR suppressor to get rid of the error message, Eval and echo are not functions, and you can't use the method call of the variable function, such as:
$func = ' eval '
$func () Such a call is invalid. It will hint that there is no eval function, and it is problematic if you define such a function yourself. Because eval is a keyword.
The eval call is similar to include, and returns null if there is no explicit return in the included file. If the direct eval we need to check the file will cause the checked file code is executed, this is not what we want, we just need to check the syntax of the file is correct. We can add a return statement before the file we want to check to get the code to jump out of the room, so the following code won't execute. All right, just do it. The code is as follows:
Copy Code code as follows:


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!";

Copy Code code as follows:

foreach:: a => B

Because the parse error cannot be handled by the Set_error_handler processing function. There is no way to catch this anomaly. So we use @ to suppress the error. The problem with this is that we can't get a detailed error message. But the only thing I need right now is to check if the syntax is correct. Not correct to recompile template file, so simple, as for syntax errors, when displaying the Web page will naturally see.
The best way to do this is to go back to PHP with this abandoned Php_check_syntax method. Next time, look at the reason why they removed the function.
Thinking is very confusing, write every point of reason, who let my Chinese so rotten.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.