1.使用命令行检查php语法错误:
php的命令行模式参考::http://www.yesky.com/imagesnew/software/php/zh/features.commandline.html
$php -l t.php
t.php代码:
-
<?php
-
echo 'this is a' test file';
-
?>
运行 php -l php之后:Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in t.php on line 2
将修改正确的代码重新运行 ,结果为:No syntax errors detected in t.php
2.
-
<?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 ";
-
echo "<br>file_content::",$file_content;
-
if(!@eval($file_content)) {
-
$error_message = "file: " .
-
realpath($file_name) . " have syntax error";
-
return false;
-
}
-
return true;
-
}
-
}
-
if(!PHP_check_syntax("b.php", $msg)) {
-
echo $msg;
-
}else {
-
echo "Woohoo, OK!";
-
}
3.php手册中有这个函数 php_check_syntax()函数 但是:注意: For technical reasons, this function is deprecated and removed
from PHP. Instead, use php -l somefile.php from the commandline.