Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

PHP error_reporting() Cheat Sheet

6 Responses »

Error handling is important in any programming language and PHP is no exception. Lucky, error handling in PHP is extremely easy to set up. The following is a quick cheat sheet for PHP, straight from PHP.net.

Error Levels

The following values and constants can be used within the error_reporting() function.

valueconstant
1E_ERROR
2E_WARNING
4E_PARSE
8E_NOTICE
16E_CORE_ERROR
32E_CORE_WARNING
64E_COMPILE_ERROR
128E_COMPILE_WARNING
256E_USER_ERROR
512E_USER_WARNING
1024E_USER_NOTICE
6143E_ALL
2048E_STRICT
4096E_RECOVERABLE_ERROR

Basic Usage

The following is basic usage of PHP's error reporting (using only one level).

//show nothing
error_reporting(0);

//show everything
error_reporting(E_ALL);

//using php.ini and ini_set()
ini_set('error_reporting', E_ALL);

Advanced Usage

The following accounts for multiple error reporting levels.

//show warnings and errors
error_reporting(E_ERROR | ERROR_WARNING);

//show all types but notices
error_reporting(E_ALL ^ E_NOTICE);

Custom Error Handling

Visit my article, Custom Error Handling in PHP, to learn about custom error handling in PHP.

Previous Error

To grab information on the last error, you can code:

//returns an array with error number, message, file, and line
error_get_last();

Discussion

  1. kenman
    March 19, 2008 @ 4:41 pm

    Just throwing this out there…..I’ve found this to be a very handy snippet which I’ve used for single pages and for complete applications both:

    In short, if the remote visitor is someone who should see errors, they will get them all, otherwise all errors are hidden. Sometimes I’ll add an extra $_GET variable check so that I can explicitly turn errors off or on, using something like $_GET['debugme']. I realize that this isn’t the best solution for large applications, but its simplicity makes it hard to beat many times.

  2. kenman
    March 19, 2008 @ 4:42 pm

    errr… lets try again with the code:

    $devIPs = array(
    gethostbyname(‘office.mycompany.com’),
    gethostbyname(‘me_at_home.dyndns.org’)
    );
    define( ‘MY_DEBUG’, in_array($_SERVER['REMOTE_ADDR'], $devIPs) );
    error_reporting(E_ALL * MY_DEBUG);

  3. March 19, 2008 @ 4:47 pm

    Good tip Ken…man!

  4. March 20, 2008 @ 1:47 am

    You may also want to the opposite, not to show any errors if they are not the dev.

  5. bignose
    April 1, 2008 @ 4:26 am

    But in the latter case (no errors if not the dev) you really should be looking at setting display_errors to off rather than altering the errors that are reported. Shouldn’t you?

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!