Home  >  Article  >  Backend Development  >  Quickly Master PHP Checking Syntax Error Techniques_PHP Tutorial

Quickly Master PHP Checking Syntax Error Techniques_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:34:151140browse

The error suppressor removes the error message. eval, like echo, is not a function and cannot be called using variable function methods. For example:

$func = 'eval'

$func () Such a call is invalid. It will prompt that there is no eval function. If you define such a function yourself, there will be a problem. Because eval is a keyword.
The eval call is similar to include. If there is no explicit return in the included file, null will be returned. If we directly eval the file we need to check, it will cause the code in the checked file to be executed. This is not what we want. We only need to check whether the syntax of the file is correct. We can add a return statement before the file to be checked to let the code jump out in advance, so that the subsequent code will not be executed. Okay, that's it. The code is as follows:

<ol class="dp-xml">
<li class="alt">
<span class="tag"><</span> ?PHP  </li><li><span>if(!function_exists('</span>PHP<span>_check_syntax')) {  </span></li><li class="alt"><span>function </span>PHP<span>_check_syntax($file_name, <br />&$</span><span class="attribute">error_message</span><span> = </span><span class="attribute-value">null</span><span>) {  </span></li><li><span>$</span><span class="attribute">file_content</span><span> = </span><span class="attribute-value">file_get_contents</span><span>($file_name);  </span></li><li class="alt"><span>$</span><span class="attribute">check_code</span><span> = </span><span class="attribute-value">"return true; ?>"</span><span>;   </span>
</li>
<li>
<span>$</span><span class="attribute">file_content</span><span> = $check_code . <br>$file_content . "</span><span class="tag"><</span><span> ?</span>PHP<span> ";  </span></li><li class="alt"><span>if(!@eval($file_content)) {  </span></li><li><span>$</span><span class="attribute">error_message</span><span> = </span><span class="attribute-value">"file: "</span><span> . <br />realpath($file_name) . " have syntax error";  </span></li><li class="alt"><span>return false;  </span></li><li><span>}  </span></li><li class="alt"><span>return true;  </span></li><li><span>}  </span></li><li class="alt"><span>}  </span></li><li><span>if(!</span>PHP<span>_check_syntax("file.</span>PHP<span>", $msg)) {  </span></li><li class="alt"><span>echo $msg;  </span></li><li><span>}  </span></li><li class="alt"><span>else {  </span></li><li><span>echo "Woohoo, OK!";  </span></li><li class="alt"><span>}  </span></li><li><span class="tag"><</span><span> ?</span>PHP<span>  </span></li><li class="alt"><span>foreach:: </span><span class="attribute">a</span><span> =</span><span class="tag">></span><span> b  </span>
</li>
<li>
<span class="tag">?></span><span>  </span>
</li>
</ol>

Because Parse error cannot be handled by the set_error_handler processing function. This exception cannot be caught. That's why @ is used to suppress errors. The problem with this is that we cannot get detailed error information. But currently the function I need is just to check whether the syntax is correct. If it is incorrect, recompile the template file. It is as simple as that. As for the syntax errors, you will naturally see them when displaying the web page.

The best way for PHP to check syntax errors is to return the abandoned PHP_check_syntax method to PHP. Next time I will study why they removed this function.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446009.htmlTechArticleThe error suppressor removes the error message. eval, like echo, is not a function and cannot be called using variable function methods. For example: A call like $func = eval $func() is invalid. It will mention...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn