PHP Error Reporting level and how to adjust

Source: Internet
Author: User
Tags php error php error reporting

When you run a PHP script, the PHP parser will do its best to report the problems it encounters. The handling behavior of error Reporting in PHP is determined by the configuration instructions in PHP configuration file php.ini. In addition, there are many levels of error reporting for PHP, which can provide a corresponding debug method based on the different error reporting levels. Once you've set up PHP to show which errors have occurred, you may want to adjust the level of error reporting. Most of the error reporting levels in PHP are listed in the following table.

If the developer wishes to report an error message to a level of error in the PHP script when it is encountered, you must set the value of the display_errors instruction to on in the configuration file php.ini, and turn on the PHP output error reporting feature. You can also call the Ini_set () function in a PHP script to dynamically set a directive in the configuration file php.ini. (Note: If Display_errors is enabled, it will realistically satisfy all error reports that have been set for the error level.) When a user visits a Web site, seeing the reality of these messages will not only lead to confusion, but also may be too much information about the server to make the server becomes very insecure. So when you enable this directive during project development or testing, you can better debug programs based on different error reports. For security and aesthetic purposes, disable the site when it is put into use).

When you are developing a site, you will want PHP to report specific types of errors that can be achieved by adjusting the level of error reporting. There are two ways to set the error reporting level.
★ By modifying the value of configuration directive error_reporting in the profile php.ini, and restarting the Web server after the modification succeeds, each PHP script can output an error report at the adjusted error level. The following is an example of modifying a php.ini configuration file that lists several ways to set different levels of values for the error_reporting directive, which can be [& (with), | ( OR) | (non)] Used with error level constants. As shown below:

★ Or you can use the error_reporting () function in PHP scripts to adjust this behavior based on each script. This function is used to determine which types of errors PHP should report within a particular page. The function gets a number or an error level constant in the table above as a parameter. As shown below:

error_reporting (0); Setting to 0 turns off error reporting completely
Error_reporting (E_all); Each error that occurs will be reported to PHP
Erroro_reporting (E_all & ~e_notice); Any non-noticeable error reports can be thrown

In the following example, we create a "note", a "warning", and a Fatal "error" in the PHP script, respectively. and by setting different error levels, limit program output to an error report that is not allowed. Create a script file named Error.php, as shown in the following code:

1234567891011121314151617 <body><?php//开启php.ini中的display_errors指令,只有该指令可以开启如果有错误报告才能输出ini_set(‘display_errors‘,1);//通过error_reporting()函数设置在本脚本中,输出所有级别的错误报告error_reporting(E_ALL);//注意(notice)的报告,不会组织脚本的执行,但是不会组织脚本的执行,并且可能不一定是一个问题getType($var); //调用函数时提供的参数变量没有在之前声明//警告(warning)的报告,指示一个问题,但是不会组织脚本的执行getType(); //调用函数时没有提供必要的参数get_Type(); //调用一个没有被定义的函数?></body>

In the above script, in order to ensure that the display_errors instruction in the configuration file is turned on, the Ini_set () function is forced to start in the script execution, and the error_repoting () function sets the error level to E_all, reporting all errors, warnings, and errors. The PHP script terminates only if it encounters an error.

"Note" and "Warning" error reports do not terminate the program's operation. If, in the output above, you do not want to have the report output of caution and warning, you can change the code in the script error_reporting () function as follows:

Modify Error_reporting (e_all&~ (e_warning|) in error.php E_notice)); Report all errors except attention and warnings

In addition to using the error_reporting and display_error two configuration directives to modify the error reporting behavior, there are many configuration directives to determine the error reporting behavior of PHP. Some other important directives are shown in the following table:

Use the Trigger_error () function to replace die ()

First the function die () is equivalent to exit (), both terminating the PHP program if executed, and can output some error reports before exiting the program. Trigger_error () can generate a user warning instead, making the program more flexible. For example, Trigger_error ("File not Found", e_user_error). Using the Trigger_error () function instead of die (), your code will have a greater advantage in handling errors and easier for the client programmer to handle errors.

>> This article fixed link:

>> reprint Please specify: Ntshongwana PHP August 04, 2014 Yuncon PHP Learning tutorial Published

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.