Check whether the PHP file has a syntax error in PHP.

Source: Internet
Author: User
Tags parse error php template

Previously, a simple template engine was used in a Dangdang project. In fact, it is also a template engine built on discuz, which is very simple, what it does is to compile some custom labels into PHP. Code . It has been said that it is very simple, so the template syntax is checked during compilation, so during the development process, the compiled PHP file has a syntax problem, and there is no relationship with the syntax problem, I just need to modify and recompile it. First, you cannot re-compile the PHP template during each request, which seriously affects the performance. During the compromise process, check whether the template file has been modified at the end of each compiled PHP file, based on the set update frequency, if you need to re-compile the template file, the problem is that the compiled PHP file has its own syntax error, and the template check step cannot be executed at all, therefore, the template file will not be re-compiled even if the template file is modified. So I want to find a simple method to check whether the generated PHP file is legal. If it is invalid, recompile it. In this way, you do not need to delete the cached file manually if an error occurs during development.

I found it online. At first, I thought that the token_get_all () function could handle syntax errors. The result showed that it only performs simple lexical analysis. No way. Then I went to the Forum and asked

Http://groups.google.com/group/professional-php/browse_thread/thread/b8581f6b07b10ff0/2601a63c406bb1c1? Lnk = GST & Q = reeze #2601a63c406bb1c1

Someone told me that I had such a function php_check_syntax () http://www.php.net/manual/en/function.php-check-syntax.php and I thought the problem was so resolute .. I should read the fuck mannual. This function is almost discarded:
Note: For technical reasons, this function is deprecated and removed from PhP. Instead, use PHP-l somefile. php from the CommandLine.

What is this technical reason? No matter what it is, and then study it slowly. You can't use this method anyway.
Their suggestion is to use the command line $ PHP-l filename. php to check the syntax.
Gary every gave me a code snippet reference:

The check in the command line is not a problem. What if I want to put it in online applications? This involves portability. First, the operating system, and then the environment variable. This will depend on the server configuration. Someone posted their own php_check_syntax () function implementation on the http://www.php.net/manual/en/function.php-check-syntax.php.
Some adopt the above command line method.
The eval method is used for verification. The eval method executes the passed-in code. If the Code has a syntax error, it throws a parser error. You can use the '@' error blocker to remove the error message. The eval and echo methods are not functions, variable functions cannot be called using methods such:
$ 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:
Checker. phpCopy codeThe 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! ";
}

File. phpCopy codeThe Code is as follows: <? 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 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.
/////// The thinking is messy. Every bit of writing is rational. Who makes my language so bad.

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: info-contact@alibabacloud.com 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.