Quick understanding of PHP checking syntax errors

Source: Internet
Author: User

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:

 
 
  1. < ?PHP  
  2. if(!function_exists('PHP_check_syntax')) {  
  3. function PHP_check_syntax($file_name, 
    &$error_message = null) {  
  4. $file_content = file_get_contents($file_name);  
  5. $check_code = "return true; ?>";   
  6. $file_content = $check_code . 
    $file_content . "< ?PHP ";  
  7. if(!@eval($file_content)) {  
  8. $error_message = "file: " . 
    realpath($file_name) . " have syntax error";  
  9. return false;  
  10. }  
  11. return true;  
  12. }  
  13. }  
  14. if(!PHP_check_syntax("file.PHP", $msg)) {  
  15. echo $msg;  
  16. }  
  17. else {  
  18. echo "Woohoo, OK!";  
  19. }  
  20. < ?PHP  
  21. foreach:: a => b  
  22. ?>  

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.


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.