What are the errors in PHP? How to deal with it?

王林
Release: 2023-02-23 13:20:01
forward
1910 people have browsed it

一、错误的级别

1.notice 提示
2.warning 警告
3.error 致命错误

notice和warning报错后继续执行,
error报错后停止执行

二、错误的提示方法

方法一:显示在浏览器上

方法二:记录在日志中

三、与错误有关的配置

在php.ini中

1. error_reporting = E_ALL:报告所有的错误
2. display_errors = On:将错误显示在浏览器上
3. log_errors = On:将错误记录在日志中
4. error_log=’地址’:错误日志保存的地址

在项目开发过程中有两个模式,开发模式,运行模式

开发模式:错误显示在浏览器上,不要记录在日志中
运行模式:错误不显示在浏览器上,记录是日志中

<?php
$debug=false;        //true:开发模式  false:运行模式
ini_set(&#39;error_reporting&#39;,E_ALL);    //所有的错误有报告
if($debug){
    ini_set(&#39;display_errors&#39;,&#39;on&#39;);    //错误显示是浏览器上
    ini_set(&#39;log_errors&#39;,&#39;off&#39;);    //错误不显示在日志中
}else{
    ini_set(&#39;display_errors&#39;,&#39;off&#39;);
    ini_set(&#39;log_errors&#39;,&#39;on&#39;);
    ini_set(&#39;error_log&#39;,&#39;./err.log&#39;);    //错误日志保存的地址
}

//测试
echo $num;
Copy after login

四、自定义错误

通过trigger_error产生一个用户级别的 error/warning/notice 信息

/**
*自定义错误处理函数
*@param $errno int 错误类别
*@param $errstr string 错误信息
*@param $errfile string 文件地址
*@param $errline int 错误行号
*/
function error($errno,$errstr,$errfile,$errline) {
    switch($errno){
        case E_NOTICE:
        case E_USER_NOTICE:
            echo &#39;记录在日志中,上班后在处理<br>&#39;;
            break;
        case E_WARNING:
        case E_USER_WARNING:    
            echo &#39;给管理员发邮件<br>&#39;;
            break;
        case E_ERROR:
        case E_USER_ERROR:
            echo &#39;给管理员打电话<br>&#39;;
            break;
    }
    echo "错误信息:{$errstr}<br>";
    echo "错误文件:{$errfile}<br>";
    echo "错误行号:{$errline}<br>";
}
set_error_handler(&#39;error&#39;);
echo $num;

//运行结果
记录在日志中,上班后在处理
错误信息:Undefined variable: num
错误文件:F:\wamp\www\4-demo.php
错误行号:50
Copy after login

想了解更多PHP相关内容,请访问PHP中文网:PHP视频教程

The above is the detailed content of What are the errors in PHP? How to deal with it?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!