PHP默认的错误处理方式:错误描述+出错文件+出错行号:
Parse error: syntax error, unexpected '=' in F:\httpd-2.4.29-Win64-VC15\Apache24\htdocs\phpstudy\18error\error.php on line 2
使用 die() 输出异常:
<?php $f = fopen("123.txt", 'r') or die("Can not open file!"); ?>
输出:
Warning: fopen(123.txt): failed to open stream: No such file or directory in F:\httpd-2.4.29-Win64-VC15\Apache24\htdocs\phpstudy\18error\die.php on line 2 Can not open file!
PHP 允许用户设置错误处理器回调,错误处理器有5个参数(前2个必须):error_level, error_message, error_file, error_line, error_context
<?php // 自定义错误 function errorHandler($error_level, $error_msg) { echo "line: $error_level, error: $error_msg"; } // 设置错误处理器 set_error_handler("errorHandler"); // 引发错误 $f = fopen("123.txt", 'r'); ?>
输出:
line: 2, error: fopen(123.txt): failed to open stream: No such file or directory
引发错误:
trigger_error()
<?php trigger_error("Error here!"); ?>
输出:
Notice: Error here! in F:\httpd-2.4.29-Win64-VC15\Apache24\htdocs\phpstudy\18error\trigger.php on line 2
PHP学习笔记 18 - 错误处理
最新推荐文章于 2021-03-24 08:00:10 发布