In PHP, check whether PHP files have syntax errors _ PHP Tutorial

Source: Internet
Author: User
Check whether the PHP file has a syntax error in PHP. 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 use a simple template engine for a custom project that was previously used in Dangdang. 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. php

The code is 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 ."
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. php

The code is as follows:


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.

...

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.