PHP error_reporting() Cheat Sheet

By  on  

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.

value constant
1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024 E_USER_NOTICE
6143 E_ALL
2048 E_STRICT
4096 E_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();

Recent Features

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    Create a CSS Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

Incredible Demos

  • By
    Chris Coyier’s Favorite CodePen Demos II

    Hey everyone! Before we get started, I just want to say it's damn hard to pick this few favorites on CodePen. Not because, as a co-founder of CodePen, I feel like a dad picking which kid he likes best (RUDE). But because there is just so...

  • By
    Spyjax:  Ajax For Evil Using Dojo

    The idea of Spyjax is nothing new. In pasts posts I've covered how you can spy on your user's history with both MooTools and jQuery. Today we'll cover how to check user history using the Dojo Toolkit. The HTML For the sake of this...

Discussion

  1. kenman

    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

    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. Good tip Ken…man!

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

  5. BigNose

    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?

  6. Thanks Man Great tip :)

  7. Error Levels has been Updated Now in new version of PHP.

  8. Art

    This is the best tool for figuring out PHP error reporting:

    http://www.bx.com.au/tools/ultimate-php-error-reporting-wizard

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!