search
HomeBackend DevelopmentPHP ProblemWhat are the errors in PHP? How to deal with 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;

四、自定义错误

通过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

想了解更多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!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software