Error reporting with PHP

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:

{code type=php}
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:
{code type=php}
// Turn off error reporting
// 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
// Another way to report all error

// An alternative way to set it in the php.ini file

ini_set(‘error_reporting’, E_ALL);
?> {/code}

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

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *