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://groups.google.com/group/professional-php/browse_thread/thread/b8581f6b07b10ff0/2601a63c406bb1c1?lnk=gst &q=reeze#2601a63c406bb1c1
I was told that there was such a function php_check_syntax () http://www.php.net/manual/en/function.php-check-syntax.php 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 http://www.php.net/manual/en/function.php-check-syntax.php.
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:
checker.php
Copy Code code 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.php
Copy Code code as follows:
<?php
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.