有时候在使用cli执行php脚本时会弹出一些杂乱的warning和notice,虽然有助于调试,但有时会影响结果输出.所以需要对错误输出级别进行设置.
error_reporting
error_reporting() 函数能够在运行时设置 error_reporting 指令。 PHP 有诸多错误级别,使用该函数可以设置在脚本运行时的级别。 如果没有设置可选参数 level, error_reporting() 仅会返回当前的错误报告级别。
1 E_ERROR 报告运行时的致命错误
2 E_WARNING 报告运行的非致命错误
4 E_PARSE 报告解析错误
8 E_NOTICE 报告通告,注意,表示所做的事情可能是错误的.
16 E_CORE_ERROR 报告PHP引擎启动失败
32 E_CORE_WARNING 报告PHP引擎启动时非致命错误
64 E_COMPILE_ERROR 报告编译错误
128 E_COMPILE_WARNING 报告编译时出现的非致命错误
256 E_USER_ERROR 报告用户触发的错误
512 E_USER_WARNING 报告用户触发的警告
1024 E_USER_NOTICE 报告用户触发的通告
2047 E_ALL 报告所有的错误和警告
2048 E_STRICT 报告不赞成的用法和不推荐的行为
错误级别的使用
error_reporting的参数可以使用数值和常量,推荐使用常量
error_reporting可以使用|设置多个错误级别,指定输出某些错误信息
error_reporting(E_ERROR | E_NOTICE | E_PARSE); //这样是不会显示Warning错误的
//不显示任何错误
//error_reporting(0);
//只显示Error级别的错误
//error_reporting(1);
//error_reporting(E_ERROR);
//报告运行时错误
//任意数目的以上选项都可以用“或”来连接(用 OR 或 |),这样可以报告所有需要的各级别错误。
error_reporting(E_ERROR | E_NOTICE | E_PARSE);//这样是不会显示Warning错误的
//报告 E_NOTICE也挺好 (报告未初始化的变量或者捕获变量名的错误拼写)
//error_reporting(E_NOTICE);
//Warning: main(): It is not safe to rely on the system's timezone settings. You a
//re *required* to use the date.timezone setting or the date_default_timezone_set(
//) function. In case you used any of those methods and you are still getting this
// warning, you most likely misspelled the timezone identifier. We selected the ti
//mezone 'UTC' for now, but please set date.timezone to select your timezone. in E
//:\PhpDemo\error_level.php on line 64
//Notice: Undefined variable: i in E:\PhpDemo\error_level.php on line 64
$j = $i / 2;
//Warning: Division by zero in E:\PhpDemo\error_level.php on line 24
$j = $j / 0;
Debug设置错误级别
define('DEBUG', true); // 在开发时,声明一个DEBUG模式
if (defined('DEBUG')) { //检测到处于开发模式
error_reporting(E_ALL); //报告所有错误
} else {
error_reporting(0);
}