Error reporting with PHP

by jeremyjones on March 8, 2011

  • Share
  • CevherShare
  • Share

When you are working on your website, you will sometimes want to change the level of error reporting that your code returns. This is a great way to get full diagnostic information so that you can fix a problem in your latest piece of code, but be careful to only show error reporting on your test site and not on your live site. The current error logging level for your site is set in php.ini like this:

error_reporting integer

The integer value used will display all error messages below that value, but the maximum error level is increasing with time as new errors are added. In order to avoid missing out any new errors, you could use a numeric value, like-1, instead of the constants E_ALL to include all errors.

As well as for the entire site, the error reporting level can be set for the individual PHP files. To do this, use the error_reporting() function like in this example:

<?php
// Turn off error reporting
error_reporting(0);
// Just get simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Change it to include notices as well
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Change it for all errors except the notices (default PHP setting)
error_reporting(E_ALL ^ E_NOTICE);
// Now get all error reports
error_reporting(E_ALL);
// Another way to report all error
error_reporting(-1);
// An alternative way to set it in the php.ini file

ini_set('error_reporting', E_ALL);
?> 

If you use the error_reporting() function without an input, it will just return the current error reporting level.

Leave a Comment

Previous post:

Next post: