百科狗-知识改变命运!
--

error_reporting() - php 错误处理日志函数

梵高1年前 (2023-11-21)阅读数 13#技术干货
文章标签错误

error_reporting()

(PHP 4, PHP 5, PHP 7)

设置应该报告何种 PHP 错误

说明

error_reporting([int $level]): int

error_reporting()函数能够在运行时设置error_reporting指令。 PHP 有诸多错误级别,使用该函数可以设置在脚本运行时的级别。如果没有设置可选参数$level,error_reporting()仅会返回当前的错误报告级别。

参数

$level

新的error_reporting级别。可以是一个位掩码也可以是一个已命名的常量。强烈建议使用已命名的常量,以确保兼容将来的版本。由于错误级别的添加、整数取值范围的增加,较久的基于整数的错误级别不会总是和预期的表现一致。

error_reporting() - php 错误处理日志函数

可用的错误级别常量及其实际含义描述在了predefined constants中。

返回值

返回旧的error_reporting级别,或者在$level参数未给出时返回当前的级别。

更新日志

版本说明
5.4.0E_STRICT成为E_ALL的一部分
5.3.0引入E_DEPRECATEDE_USER_DEPRECATED
5.2.0引入E_RECOVERABLE_ERROR
5.0.0引入E_STRICT(但不包括在E_ALL之内)。

范例

Example #1error_reporting()范例

注释

Warning

虽然error_reporting增强了包含E_STRICT错误的能力(反之亦然),但大多数E_STRICT的错误是在编译时被评估的,所以不会在文件中被报告。

Tip

传入-1的值将尽可能显示所有错误,甚至包括将来 PHP 可能加入的新的错误级别和常量。至 PHP 5.4,常量E_ALL有同样的行为。

参见

  • display_errors指令
  • html_errors指令
  • xmlrpc_errors指令
  • ini_set() 为一个配置选项设置值
If you just see a blank page instead of an error reporting and you have no server access so you can't edit php configuration files like php.ini try this:
- create a new file in which you include the faulty script:

- execute this file instead of the faulty script file 
now errors of your faulty script should be reported.
this works fine with me. hope it solves your problem as well!
The example of E_ALL ^ E_NOTICE is a 'bit' confusing for those of us not wholly conversant with bitwise operators.
If you wish to remove notices from the current level, whatever that unknown level might be, use & ~ instead:

^ is the xor (bit flipping) operator and would actually turn notices *on* if they were previously off (in the error level on its left). It works in the example because E_ALL is guaranteed to have the bit for E_NOTICE set, so when ^ flips that bit, it is in fact turned off. & ~ (and not) will always turn off the bits specified by the right-hand parameter, whether or not they were on or off.
The error_reporting() function won't be effective if your display_errors directive in php.ini is set to "Off", regardless of level reporting you set. I had to set
display_errors = On
error_reporting = ~E_ALL
to keep no error reporting as default, but be able to change error reporting level in my scripts.
I'm using PHP 4.3.9 and Apache 2.0.
Some E_STRICT errors seem to be thrown during the page's compilation process. This means they cannot be disabled by dynamically altering the error level at run time within that page.
The work-around for this was to rename the file and replace the original with a error_reporting() call and then a require() call.
Ex, rename index.php to index.inc.php, then re-create index.php as:

That allows you to alter the error reporting prior to the file being compiled.
I discovered this recently when I was given code from another development firm that triggered several E_STRICT errors and I wanted to disable E_STRICT on a per-page basis.
If you are using the PHP development server, run from the command line via `php -S servername:port`, every single error/notice/warning will be reported in the command line itself, with file name, and line number, and stack trace.
So if you want to keep a log of all the errors even after page reloads (for help in debugging, maybe), running the PHP development server can be useful.
If you want to see all errors in your local environment, you can set your project URL like "foo.com.local" locally and put that in bootstrap file. 
It could save two minutes to someone:
E_ALL & ~E_NOTICE integer value is 6135
To enable error reporting for *ALL* error messages including every error level (including E_STRICT, E_NOTICE etc.), simply use: 
I always code with E_ALL set.
After a couple of pages of

I made this function to make things a little bit quicker. Unset values passed by reference won't trigger a notice. 
In php7, what was generally a notice or a deprecated is now a warning : the same level of a mysql error … unacceptable for me.
I do have dozen of old projects and I surely d'ont want to define every variable which I eventually wrote 20y ago.
So two option: let php7 degrade my expensive SSDs writing Gb/hours or implement smthing like server level monitoring ( with auto_[pre-ap]pend_file in php.ini) and turn off E_WARNING
Custom overriding the level of php errors should be super handy and flexible …
If you get a weird mysql warnings like "Warning: mysql_query() : Your query requires a full tablescan...", don't look for error_reporting settings - it's set in php.ini.
You can turn it off with
ini_set("mysql.trace_mode","Off");
in your script
http://tinymy.link/mctct
This article refers to these two reporting levels:
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
What is the difference between those two levels?
Please update this article with a clear explanation of the difference and the possible use cases.
I had the problem that if there was an error, php would just give me a blank page. Any error at all forced a blank page instead of any output whatsoever, even though I made sure that I had error_reporting set to E_ALL, display_errors turned on, etc etc. But simply running the file in a different directory allowed it to show errors!
Turns out that the error_log file in the one directory was full (2.0 Gb). I erased the file and now errors are displayed normally. It might also help to turn error logging off.
https://techysupport.co/norton-tech-support/
To expand upon the note by chris at ocproducts dot com. If you prepend @ to error_reporting(), the function will always return 0. 
Only display php errors to the developer...

Just replace 00.00.00.00 with your ip address.
Php >5.4
Creating a Custom Error Handler
set_error_handler("customError",E_ALL);
function customError($errno, $errstr)
 {
 echo "Error: [$errno] $errstr
"; echo "Ending Script"; die(); }
Remember that the error_reporting value is an integer, not a string ie "E_ALL & ~E_NOTICE". 
This is very useful to remember when setting error_reporting levels in httpd.conf:
Use the table above or: 

To get the appropriate integer for your error-level. Then use:
php_admin_value error_reporting YOUR_INT
in httpd.conf
I want to share this rather straightforward tip as it is rather annoying for new php users trying to understand why things are not working when the error-level is set to (int) "E_ALL" = 0...
Maybe the PHP-developers should make ie error_reporting("E_ALL"); output a E_NOTICE informative message about the mistake?
see more information about php 5.3 deprecated errors
http://php.net/manual/en/migration53.deprecated.php
this is to show all errors for code that may be run on different versions
for php 5 it shows E_ALL^E_STRICT and for other versions just E_ALL
if anyone sees any problems with it please correct this post 
error_reporting() may give unexpected results if the @ error suppression directive is used.

config.php

will throw an error level E_WARNING in relation to the non-existent file (depending of course on your configuration settings). If the suppressor is removed, this works as expected.
Alternatively using ini_set('display_errors', 0) in config.php will achieve the same result. This is contrary to the note above which says that the two instructions are equivalent.
The error_reporting() function will return 0 if error suppression is currently active somewhere in the call tree (via the @ operator).
error_reporting() has no effect if you have defined your own error handler with set_error_handler()
[Editor's Note: This is not quite accurate.
E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR and E_COMPILE_WARNING error levels will be handled as per the error_reporting settings.
All other levels of errors will be passed to the custom error handler defined by set_error_handler().
Zeev Suraski suggests that a simple way to use the defined levels of error reporting with your custom error handlers is to add the following line to the top of your error handling function:
if (!($type & error_reporting())) return;
 -zak@php.net]
It might be a good idea to include E_COMPILE_ERROR in error_reporting. 
If you have a customer error handler that does not output warnings, you may get a white screen of death if a "require" fails.
Example:

To prevent this, simply include E_COMPILE_ERROR in the error_reporting. 
Note that E_NOTICE will warn you about uninitialized variables, but assigning a key/value pair counts as initialization, and will not trigger any error : 
In phpinfo() error reporting level display like a bit (such as 4095)
Maybe it is a simply method to understand what a level set on your host
if you are not have access to php.ini file

In $res you will have all constants of error reporting
$res[]=int(16) // E_CORE_ERROR 
$res[]=int(8)  // E_NOTICE 
...

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)