DevChunks

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
{/code}

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}
<?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);
?> {/code}

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

Share:

Leave a Reply

Your email address will not be published.

More Posts

How to use PHP Type Declarations

PHP has developed into a more strictly typed language in recent years. Type Declarations help with that and are a fairly new addition to PHP,

Iteration

If you have come to JavaScript from a background in Java or C, then you will like the variation on the for loop. It is

Context Object Manipulation

One of the key concepts of JavaScript is the keyword this, which signifies the JavaScript context object. If you write an inner function, you can access

Polymorphic Functions

It is possible to define multiple functions with the same name in JavaScript, and the last one of those that you define will be the