Home php教程 php手册 PHP中error_reporting()用法详解

PHP中error_reporting()用法详解

Jun 06, 2016 pm 07:45 PM
php

php中我们对错误的处理会常用到error_reporting函数了,大家可以看到最多的是error_reporting(E_ALL ^ E_NOTICE)了,这个到底什么意思呢,下面我来来看看。

error_reporting() 函数规定报告哪个错误 。该函数设置当前脚本的错误报告级别。该函数返回旧的错误报告级别。

首先要知道error_reporting()函数是用来设置错误级别并返回当前级别的。它有14个错误级别,如下:

1 E_ERROR 致命的运行时错误。 错误无法恢复过来。脚本的执行被暂停 2 E_WARNING 非致命的运行时错误。 脚本的执行不会停止 4 E_PARSE 编译时解析错误。解析错误应该只由分析器生成 8 E_NOTICE 运行时间的通知。 16 E_CORE_ERROR 在PHP启动时的致命错误。这就好比一个在PHP核心的E_ERROR 32 E_CORE_WARNING 在PHP启动时的非致命的错误。这就好比一个在PHP核心E_WARNING警告 64 E_COMPILE_ERROR 致命的编译时错误。 这就像由Zend脚本引擎生成了一个E_ERROR 128 E_COMPILE_WARNING 非致命的编译时错误,由Zend脚本引擎生成了一个E_WARNING警告 256 E_USER_ERROR 致命的用户生成的错误。 512 E_USER_WARNING 非致命的用户生成的警告。 1024 E_USER_NOTICE 用户生成的通知。 2048 E_STRICT 运行时间的通知。 4096 E_RECOVERABLE_ERROR 捕捉致命的错误。 8191 E_ALL来 所有的错误和警告。

好像php默认是不开启错误的,所以你需要配置php.ini文件:

将 display_errors = Off 改为display_errors = On

另外还要配置错误级别:将

error_reporting = E_ALL     改为:

error_reporting = E_ALL & ~E_NOTICE

应为php默认是显示所有错误的,而有些无害的提示我们不需要显示,所以设置如上!

也可以在php代码运用如下:

使用示例:

今天学习CI框架过程中遇到个问题:

A PHP Error was encountered Severity: Notice Message: Undefined variable: user

一般在默认的普通PHP文件中输出一个未定义声明的变量是不会报错误的,但在codeigniter框架下却要报错误,这对于想集成 添加 和 修改 页面于一体的”懒人”很不方便,由于是初学者开始还想怎么在代码中屏蔽这一错误提示呢.甚至用到了@,但听很多人都说@会大大降低性能….

最后突然想到,是不是codeigniter有意让这错误信息提示出来了呢,我们该如何去屏蔽掉这一类错误呢无意中搜索到了”如何让codeigniter不显示Notice信息?”,茅塞顿开.原来是入口index.php中的error_reporting(E_ALL);在作怪.只需要把它改成
  error_reporting(E_ALL ^ E_NOTICE);
就可以屏蔽掉这个错误,而不影响其他的报错.

我们在程序中可能经常看到这么一个函数

function setErrorReporting() { //从配置文件读取当前是否为开发环境 if (DEV_ENV == true) { ini_set("error_reprorting", "E_ALL & ~E_NOTICE"); ini_set("display_errors", "on"); } else { error_reporting(E_ALL); ini_set('display_errors', 'Off'); ini_set("log_errors" , "On"); ini_set('error_log', '/var/log/phperror.log'); } }

举例说明:
在Windows环境下:原本在php4.3.0中运行正常的程序,在4.3.1中为何多处报错,,大体提示为:Notice:Undefined varialbe:变量名称.

例如有如下的代码:
 代码如下 复制代码
if (!$tmp_i) {
$tmp_i=10;
}
在4.3.0中运行正常,在4.3.1中运行会提示Notice:Undefined varialbe:tmp_i
问题如下:1.问题出在哪里?
2.应如何修改这段代码?
3.不改段代码,如何修改php.ini中的设置使原来在4.3.0中的程序在4.3.1的环境下运行正常而不出现这个错误提示.
解决办法:

在程序开头加一句:
 代码如下 复制代码
error_reporting(E_ALL & ~E_NOTICE); 或error_reporting(E_ALL ^ E_NOTICE);
或者修改php.ini:
 代码如下 复制代码
error_reporting = E_ALL & ~E_NOTICE
有关error_reporting()函数: error_reporting() 设置 PHP 的报错级别并返回当前级别。
; 错误报告是按位的。或者将数字加起来得到想要的错误报告等级。
; E_ALL - 所有的错误和警告
; E_ERROR - 致命性运行时错
; E_WARNING - 运行时警告(非致命性错)
; E_PARSE - 编译时解析错误
; E_NOTICE - 运行时提醒(这些经常是是你的代码的bug引起的,也可能是有意的行为造成的。(如:基于未初始化的变量自动初始化为一个空字符串的事实而使用一个未初始化的变量)
; E_CORE_ERROR - 发生于PHP启动时初始化过程中的致命错误
; E_CORE_WARNING - 发生于PHP启动时初始化过程中的警告(非致命性错)
; E_COMPILE_ERROR - 编译时致命性错
; E_COMPILE_WARNING - 编译时警告(非致命性错)
; E_USER_ERROR - 用户产生的出错消息
; E_USER_WARNING - 用户产生的警告消息
; E_USER_NOTICE - 用户产生的提醒消息
E_NOTICE 表示一般情形不记录,只有程式有错误情形时才用到,例如企图存取一个不存在的变数,或是呼叫 stat() 函式检视不存在的档案。
E_WARNING 通常都会显示出来,但不会中断程式的执行。这对除错很有效。例如:用有问题的常规表示法呼叫 ereg()。
E_ERROR 通常会显示出来,亦会中断程式执行。意即用这个遮罩无法追查到记忆体配置或其它的错误。
E_PARSE 从语法中剖析错误。
E_CORE_ERROR 类似 E_ERROR,但不包括 PHP 核心造成的错误。
E_CORE_WARNING 类似 E_WARNING,但不包括 PHP 核心错误警告

使用方法:
error_reporting(0);//禁用错误报告
error_reporting(E_ALL ^ E_NOTICE);//显示除去 E_NOTICE 之外的所有错误信息
error_reporting(E_ALL^E_WARNING^E_NOTICE);//显示除去E_WARNING E_NOTICE 之外的所有错误信息
error_reporting(E_ERROR | E_WARNING | E_PARSE);//显示运行时错误,与error_reporting(E_ALL ^ E_NOTICE);效果相同。error_reporting(E_ALL);//显示所有错误
error_reporting(0)
error_reporting(255);
是列出所有提示
error_reporting(0);
是不显示所有提示
建议使用
error_reporting(7);
只显示严重错误
1 E_ERROR 致命的运行时错误
2 E_WARNING 运行时警告(非致命性错误)
4 E_PARSE 编译时解析错误
8 E_NOTICE 运行时提醒(经常是bug,也可能是有意的)
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 用户自定义的提醒(经常是bug,也可能是有意的)
2048 E_STRICT 编码标准化警告(建议如何修改以向前兼容)
4096 E_RECOVERABLE_ERROR 接近致命的运行时错误,若未被捕获则视同E_ERROR
6143 E_ALL 除E_STRICT外的所有错误(PHP6中为8191,即包含所有)

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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles