Home > Backend Development > PHP Tutorial > Usage example of php error_reporting() function (error capturing)

Usage example of php error_reporting() function (error capturing)

WBOY
Release: 2016-07-25 09:04:16
Original
1108 people have browsed it
  1. display_errors = Off
  2. error_reporting = E_ALL
Copy code

The current default values ​​of these two variables can be found by searching for them in the php.ini file. The purpose of the display_errors variable is obvious - it tells PHP whether to display errors. The default value is Off. However, to make the development process easier, set this value to On:

  1. display_errors = On
Copy code

The default value of the error_reporting variable is E_ALL. This setting will show everything from bad coding practices to harmless tips to errors. E_ALL is a bit too granular for development purposes, as it also displays hints on the screen for small things (such as variables not being initialized), which messes up the browser's output. I only want to see errors and bad coding practices, but not harmless tips. So, please replace the default value of error_reporting with the following value:

  1. error_reporting = E_ALL & ~E_NOTICE
Copy the code

Restart Apache and you’re all set. Next, you'll learn how to do the same thing on Apache.

2. Error report on the server

Depending on what Apache is doing, turning on error reporting in PHP may not work since there may be multiple versions of PHP on the computer. Sometimes it's difficult to tell which PHP version Apache is using because Apache can only look at one php.ini file. Not knowing which php.ini file Apache is using to configure itself is a security issue. However, there is a way to configure PHP variables in Apache so that the correct error level is set.

 Also, it’s good to know how to set these configuration variables on the server side to override or preempt the php.ini file, thus providing a higher level of security. When configuring Apache, you should have already touched the basic configuration in the http.conf file in /conf/httpd.conf.

To do what you have already done in the php.ini file, add the following lines to httpd.conf, overwriting any php.ini files:

  1. php_flag display_errors on
  2. php_value error_reporting 2039
Copy code

This will overwrite the flags already set for display_errors in the php.ini file, as well as the value of error_reporting. The value 2039 represents E_ALL & ~E_NOTICE. If you prefer to use E_ALL, set the value to 2047. Again, you still need to restart Apache.

Next, we need to test the error reporting on the server.

Regarding the error_reporting() function, it can shield some error messages, but errors caused by the PHP core cannot be shielded, because errors caused by the PHP core will directly cause the PHP file compilation to fail, because the writing format does not follow PHP's Errors caused by coding rules cannot be blocked.

  1. * For now, avoid warnings of E_STRICT mode
  2. * (this must be done before function definitions)
  3. */
  4. if (defined('E_STRICT')) {
  5. $old_error_reporting = error_reporting (0);
  6. if ($old_error_reporting & E_STRICT) {
  7. error_reporting($old_error_reporting ^ E_STRICT);
  8. } else {
  9. error_reporting($old_error_reporting);
  10. }
  11. unset($old_error_reporting);
Copy code

Common ones are as follows:

  1. // Turn off all error reporting; Turn off all errors

  2. error_reporting(0);

  3. // Report simple running errors; Report a simple running errors Running errors

  4. error_reporting(E_ERROR | E_WARNING | E_PARSE);

  5. // Reporting E_NOTICE can be good too (to report uninitialized

  6. // variables or catch variable name misspellings …); including reporting some uninitialized
  7. // variables or catch variable name misspellings …); Initialized variables or catching typos in variable names
  8. error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

  9. // Report all errors except E_NOTICE

  10. // This is the default value set in php .ini;Report all errors but excluding E_NOTICE This is also the default setting of php.ini
  11. error_reporting(E_ALL ^ ​​E_NOTICE);

  12. // Report all PHP errors (bitwise 63 may be used in PHP 3);Report all errors

  13. error_reporting(E_ALL);

  14. // Same as error_reporting(E_ALL); Same as above

  15. ini_set('error_reporting', E_ALL);

Copy code


🎜
source:php.cn
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