error_reportingDescriptionerror_reporting() sets which level(s) of PHP errors are reported within the current document and returns the previous level of error reporting. The new settings take effect immediately after the function call is made. If error_reporting() is called without any arguments, the current level of error reporting is returned. The error reporting level can be set with either an integer or a named constant. Multiple integers or constants can be combined with bitwise operators. Any PHP bitwise operator can be used (&, |, ~, ^, << and >>).
NoteThe use of named constants is strongly recommended. While the integer values that represent various error states may change as time passes, the named constants will be much more stable.
NoteIf you have defined a custom error handler using set_error_handler() , then the level of error reporting set using the error_reporting() function or configuration directive will only affect the E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, and E_COMPILE_WARNING error levels. All other error levels are expected to be handled by the custom error handler. ExampleExample 270. Basic use of error_reporting() // Enable every level of error reporting error_reporting (E_ALL); // Enable the E_ERROR, E_WARNING, and E_PARSE error reporting levels error_reporting (E_ERROR | E_WARNING | E_PARSE); // Disable all levels of error reporting. // Usually this is a bad idea. To prevent errors from being displayed, // disable the display_errors directive in the php.ini file or via ini_set // i.e. ini_set ('display_errors', 0); error_reporting (0); // Remove E_NOTICE from the current level of error reporting. // Use error_reporting to fetch the current level of error reporting. error_reporting (error_reporting (0) & ~ E_NOTICE); Example 271. Use error_reporting() to help debug code <?php // Make a handy class that simplifies calling the debugging code // multiple times within the same script. class debug { // Define variables that store the old error reporting and logging states var $old_error_level; var $old_display_level; var $old_error_logging; var $old_error_log; // For storing the path to the temporary log file var $debug_log; function debug ($log = 'debug.log') { $this->debug_log = $log; } function start () { // Show all errors $this->old_error_level = error_reporting (E_ALL); // Make sure that the errors get displayed $this->old_display_level = ini_set ('display_errors', 1); // Make sure that error logging is enabled $this->old_error_logging = ini_set ('log_errors', 1); // Make sure that the errors get logged to a special log file $this->old_log_setting = ini_set ('error_log', $this->debug_log); } function stop () { // Use the stored error and display settings to // restore the previous state error_reporting ($this->old_error_level); ini_set ('display_errors', $this->old_display_level); ini_set ('log_errors', $this->old_error_logging); ini_set ('error_log', $this->debug_log); } } // Instantiate the class so that we can use it $debug = new debug (); // Start debugging $debug->start (); // Code to debug // The line below should generate an undefined variable warning print $foo . 'Hello '; // Stop debugging and restore previous error handling and logging values $debug->stop (); // Even though $bar is undefined, unless you have E_NOTICE enabled, no error will be generated print $bar . 'World'; ?>
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|