检查php语法错误,PHP检查语法错误技巧
1.使⽤命令⾏检查php语法错误:
$php -l t.php
t.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);
//echo $file_content;
$check_code = "return true; ?>";
$file_content = $check_code .$file_content . "<?php ";
echo "
file_content::",$file_content;
if(!@eval($file_content)) {
$error_message = "file: " .
realpath($file_name) . " have syntax error";
parse 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.