How to turn on error reporting in PHP

If I had a penny for every time I've Googled this...

For my own future reference if not everyone elses, here's the magic two lines to get the errors showing up for your site - just place this at the top of the PHP file you want to debug.


If you want to know a little more on what the above does and your other options, keep reading...

A guide to PHP error reporting

Why do I need to switch it on?

By default, PHP error reporting will almost always be switched off in the PHP.ini file as a security precaution to prevent revealing too much information to potential attackers. The lines we're using above are a method of overriding these settings without directly changing the ini file. 

If you did want to switch this on directly in the ini file - for example on a local testing machine - then you would be looking to change the following line:

error_reporting = E_ALL

If you did implement this and then wanted to selectively turn error reporting off, you could do so by adding the following to your PHP files:


The error_reporting function

So what does this function actually do? Essentially error_reporting() will define what level of errors should be printed on screen while the current PHP script is being executed. It takes in one argument which is the error level. 

Reporting levels

Here are the main options you should be aware of:

E_ERROR - Fatal errors, i.e. the script cannot continue to run. This might occur by referencing a function that does exist, thus impossible to continue.

E_WARNING - Non-fatal errors, these could quite plausibly be harmless and ignored, though a good application should be completely error and warning free. An example is if you had a foreach loop to iterate through an array but didn't check that array actually had anything in it first, this would cause a warning.

E_PARSE - For errors that occur while parsing, for example a syntax error would trigger a 'cannot parse file' error. 

E_NOTICE - These errors serve more as advice for potential problems or bad practice. For example you could reference an element in an array using $array[value] rather than $array['value']. While this would work, it could conflict with a constact defined as 'value' and thus is not best practice and has the potential to cause problems.

E_STRICT - These errors serve as suggestions to improve better code compatability. As an example, you would receive an E_STRICT error if trying to call a non-static method statically. 

E_ALL - No guesses for what this one does! All errors and warnings are shown with the exception of E_STRICT.

Selectively switch on / off reporting levels

To pick and choose which of these error reporting levels you wish to use (assuming you don't just want all of them, or one of them, you could use one of the following:

error_reporting( E_ERROR | E_WARNING );

The pipe symbol above acts as an 'or' to show the E_ERROR and E_WARNING errors.

error_reporting( E_ALL & ~E_NOTICE );

The ampersand and tilde allow you to create an 'except' argument. In the above all errors except E_NOTICE will be shown (or E_STRICT as this isn't part of E_ALL!).

Overriding display_errors

The other setting we change is 'display_errors', which is set to 1 (for true). This will override the PHP.ini file to actually present the level of error reporting we've requested. 

Now go fix those errors...

Sign Up

NEXT: PHP print_r into a variable

How to return the print_r result into a variable.

comments powered by Disqus

Popular Tags

Need a web developer?

If you'd like to work with code synthesis on your next project get in touch via the contact page.