您的位置:首页 > 编程语言 > PHP开发

PHP检查语法错误技巧

2010-11-12 23:08 351 查看
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);
//echo $file_content;
$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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: