错误分类
- 语法错误:程序不能运行,是在运行之前,检查语法的时候,就发现语法出错,结果是提示错误,不运行程序。
- 运行时错误:语法检查没错,然后开始运行,在运行中出现了错误,然后报错。这是开发中最常见的错误。
- 逻辑错误:程序能运行,且一直到结束没有报错,但执行得到的结果却是错的。
常见错误代号(重点)
含义:是指在程序运行时,发生的错误,系统会针对每种错误,给出相应的错误代号,并进行提示(报错)。
另外,程序如果在运行之前检查语法的时候就发现语法错误,也会报错,也有一个错误代号。
常见错误代号有:
- E_NOTICE:提示性错误,轻微;错误发生后,后面的程序继续执行。
- E_WARNING:警告性错误,稍微严重;错误发生后,后面的程序继续执行。
- E_ERROR:严重错误/致命错误;错误发生后,后面的程序不再执行!
- E_PARSE:语法错误(语法解析错误);语法解释错误,是直接就不运行程序。
- E_USER_NOTICE:用户自定义的提示错误。
- E_USER_WARNING:用户自定义的警告错误
- E_USER_ERROR:用户自定义的严重错误
- E_ALL:它是一个代表“所有”错误的代号。
//先认识一下几个错误代号:
echo "<br>E_NOTICE: " . E_NOTICE;
echo "<br>E_WARNING: " . E_WARNING;
echo "<br>E_ERROR: " . E_ERROR;
说明:
- 这些错误代号,其实只是系统预先设定的一些常量,他们的值大约是:1, 2, 4, 8,16.....(官方文档:https://www.php.net/manual/zh/reserved.constants.php)
- 这些错误代号,通常只是用于对错误控制时进行“设置”使用。
- 他们是一系列的整数,并具有一定的规律:1,2,4,8,16,32,64, 。。。。
- 可以在php.ini中使用(设置)他们,如下所示:
; 所有PHP错误都显示(默认设置)
error_reporting = E_ALL
; PHP ERROR错误显示
error_reporting = E_ERROR
; PHP NOTICE错误显示
error_reporting = E_NOTICE
注释提示:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This directive informs PHP of which errors, warnings and notices you would like
; it to take action for. The recommended way of setting values for this
; directive is through the use of the error level constants and bitwise
; operators. The error level constants are below here for convenience as well as
; some common settings and their meanings.
; By default, PHP is set to take action on all errors, notices and warnings EXCEPT
; those related to E_NOTICE and E_STRICT, which together cover best practices and
; recommended coding standards in PHP. For performance reasons, this is the
; recommend error reporting setting. Your production server shouldn't be wasting
; resources complaining about best practices and coding standards. That's what
; development servers and development settings are for.
; Note: The php.ini-development file has this setting as E_ALL. This
; means it pretty much reports everything which is exactly what you want during
; development and early testing.
;
; Error Level Constants:
; E_ALL - All errors and warnings (includes E_STRICT as of PHP 5.4.0)
; E_ERROR - fatal run-time errors
; E_RECOVERABLE_ERROR - almost fatal run-time errors
; E_WARNING - run-time warnings (non-fatal errors)
; E_PARSE - compile-time parse errors
; E_NOTICE - run-time notices (these are warnings which often result
; from a bug in your code, but it's possible that it was
; intentional (e.g., using an uninitialized variable and
; relying on the fact it is automatically initialized to an
; empty string)
; E_STRICT - run-time notices, enable to have PHP suggest changes
; to your code which will ensure the best interoperability
; and forward compatibility of your code
; E_CORE_ERROR - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's
; initial startup
; E_COMPILE_ERROR - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR - user-generated error message
; E_USER_WARNING - user-generated warning message
; E_USER_NOTICE - user-generated notice message
; E_DEPRECATED - warn about code that will not work in future versions
; of PHP
; E_USER_DEPRECATED - user-generated deprecation warnings
;
; Common Values:
; E_ALL (Show all errors, warnings and notices including coding standards.)
; E_ALL & ~E_NOTICE (Show all errors, except for notices)
; E_ALL & ~E_NOTICE & ~E_STRICT (Show all errors, except for notices and coding standards warnings.)
; E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors)
; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
; Development Value: E_ALL
; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
; http://php.net/error-reporting
//演示3种常见的错误:
ini_set("display_errors", 1); //
//ini_set("error_reporting",E_NOTICE | E_ERROR);
ini_set("error_reporting",E_WARNING);
echo $v1;
echo "<h3>111</h3>";
include './no-this-file.php';
echo "<h3>222</h3>";
$n2 = raund(2.6);
echo "<h3>333</h3>";
错误触发
就是发生了一个错误的意思——即触发了错误。
有两种情形会触发错误:
- 程序本身有错,则运行时就会触发错误(并提示)。
- 程序本身没错,但出现不符合预计的情形(比如数据不符合要求)。
此时程序员可以主动触发一个错误,也可以说是由程序员“主动创建一个错误”——这就是“用户错误”,包括:
- E_USER_NOTICE:
- E_USER_WARNING:
- E_USER_ERROR:
如何触发“用户错误”呢?自定义错误触发语法:
trigger_error(“自定义错误提示内容”, 自定义错误的代号);
案例演示:
//演示“用户错误”的触发
//输入一个年龄(简化为变量值),如果年龄超过60或小于0,就触发一个notice错误!
function reg_user( $a ){
if($a > 60 || $a < 0)
{
//trigger_error("我们自己触发的错误的提示信息", 用户错误代号);
trigger_error("年龄不在我们公司的投保范围中", E_USER_NOTICE);
}
else{
echo "<br>用户资料审核通过!";
}
}
$age = 18;
reg_user($age);
echo "<br>再来一个需要审核的用户:";
reg_user(66);
错误显示设置
如果有错误发生(触发了错误),默认情况下会被显示在页面(即输出的结果页面)。
我们可以对此进行设置,以决定以下两点:
1,设置display_errors以决定是否显示错误:
在php.ini中设置: display_errors = On或Off,这里设置,影响所有使用该php语言引擎的代码(网站页面);
; display_errors ; Default Value: On ; Development Value: On ; Production Value: Off
在php文件中设置:在这里设置,只影响当前网页代码本身。
ini_set(‘display_errors’, 1); //1表示显示,0不显示
2,设置error_reporting
以决定显示哪些错误:
- 在php.ini中设置: error_reporting = 错误代号1 | 错误代号2 | .....//(要显示的就写出来,或者可以写E_ALL, 表示显示所有)
- 在代php文件中,道理类似:
ini_set(‘error_reporting’, 错误代号1 | 错误代号2| ...)
错误日志设置
如果有错误发生(触发了错误),默认情况下不会将错误信息记录(保存)下来。
我们可以对此进行设置,以决定以下两点:
1,设置log_errors以决定是否记录错误:
- php.ini中设置: log_errors = On 或 Off
- 代码文件中设置: ini_set (‘log_errors’, 1或0)
2,设置error_log以决定记录到哪里:
通常,就设置为一个文件名,php系统会在网站的每个文件夹下都建立该文件,并记录错误。
- php.ini中: error_log = error.txt; //它是纯文本的
- 代码中: ini_set(“error_log”, ‘error.txt’);
演示代码:
//演示错误日志的记录:
//设置需要去记录错误信息:
ini_set("log_errors", 1);
//设置记录错误信息的文件
ini_set('error_log', './error_log.txt');
echo $v1;
echo "<h3>111</h3>";
include './no-this-file.php';
echo "<h3>222</h3>";
$n2 = raund(2.6);
echo "<h3>333</h3>";
error_log.txt
文件内容
[07-Jun-2020 10:50:37 PRC] PHP Notice: Undefined variable: v1 in C:\Users\myxc\Documents\wamp\apache\htdocs\jd\16error-log.php on line 9
[07-Jun-2020 10:50:37 PRC] PHP Warning: include(./no-this-file.php): failed to open stream: No such file or directory in C:\Users\myxc\Documents\wamp\apache\htdocs\jd\16error-log.php on line 12
[07-Jun-2020 10:50:37 PRC] PHP Warning: include(): Failed opening './no-this-file.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\myxc\Documents\wamp\apache\htdocs\jd\16error-log.php on line 12
[07-Jun-2020 10:50:37 PRC] PHP Fatal error: Uncaught Error: Call to undefined function raund() in C:\Users\myxc\Documents\wamp\apache\htdocs\jd\16error-log.php:15
Stack trace:
#0 {main}
thrown in C:\Users\myxc\Documents\wamp\apache\htdocs\jd\16error-log.php on line 15
自定义错误处理(重点/难点)
之前,我们面对的情形都是错误发生的时候,系统生成错误,并处理错误(给出错误信息)。
我们能控制的就只是:是否显示,显示什么,是否记录,记录到哪里?
实际上,我们也可以更进一步控制错误信息,以决定错误发生的时候,显示什么样的错误信息。
这就是“自定义错误处理”。
具体做法,分2步:
- 第1步:声明错误发生时,由我们自己来处理——设定一个错误处理的函数名。
- 第2步:定义该函数,在函数中详细设定错误的处理情况:怎么显示,显示什么,怎么记录,记录什么。
演示代码:
//自定义错误,分2步:
//1,声明,我们自己使用自己的函数来处理错误
//set_error_handler("处理错误的自己的函数名");
set_error_handler("my_error_handler");
//2,定义该函数!
function my_error_handler($errCode, $errMsg, $errFile, $errLine)
//参数解释:errCode错误代号,errMsg错误信息,errfile错误文件,errline行号
//此形参顺序固定,而且是由系统会调用该函数并传入实参数据!
{
//此函数中,我们就可以去自己显示有关错误信息,和记录信息
$str = "<p>大事不好了,发生错误了,快来人啊。。。";
$str .= "<br>发生时间:" . date('Y-m-d H:i:s');
$str .= "<br>错误代号:" . $errCode;
$str .= "<br>错误信息:" . $errMsg;
$str .= "<br>错误文件:" . $errFile;
$str .= "<br>错误行号:" . $errLine;
$str .= "</p>";
echo $str;
//也可以在这里继续去“记录错误”——就是错误日志
//FILE_APPEND表示该函数使用“追加模式”来写入数据
file_put_contents("./error.html", $str, FILE_APPEND);
}
//先给出几个出错的代码:
echo "<br>v1=$v1"; //未定义的变量
include './no-this-file.php'; //载入失败
function I1(){}
l1(); //调用不存在的函数;
echo "<p>最后的段落</p>";
演示案例:运算符详解、计算机码介绍