Error Reporting

Once you start to write more than a couple of lines of PHP errors will undoubtedly creep in and the more code there is the harder it is to find the problem. PHP has a brilliant Error Reporting system that pinpoints the area if not the line where the problem is and also can tell you either what is missing or give you an idea of what has gone wrong.

The Error Reporting is usually turned on by default however a lot of online hosts switch this off to minimal reporting as some errors are just really warnings anyhow and won’t actually break a page. When testing for yourself however it’s good to know exactly what is happening and where. So if you’re working offline on your own PC then you will need to ensure that the Error Reporting is set to All in the php.ini file (see the PHP documentation for this), alternatively you can put a function at the top of every page (or use an include!) to ensure it’s always running and then remove it when you’re happy with your site. The function to set all errors to on is:


There are further values that the function can take which are listed on the site however I won’t go into those.

The error suppression operator can be used in front of any expression that generates or contains a value. This is required to block errors for example going back to our form, if box1 did not have anything in it then when you came to print the variable $box1 and error would appear on screen. By putting an @ sign before the word ‘print’ it suppresses it, therefore if an error occurs, don’t display it.

[source:php]@ print 5/0; // of course you cannot divide by zero[/source]

The Error suppression operator is handy for a quick fix and is used quite often however to have full complete code you shouldn’t be suppressing the error but acting on it. For example the above print statement where 5 is divided by zero. Perhaps this was a calculation for a shopping basket script. Suddenly you realise you need to know if that is happening as there’s a major flaw if it does. You could use an if statement to check for an error, and if there is an error prevent the script from processing further and contact you via an email (using the mail() function).

However whilst I’ve been told that using the above suppression method is a bad idea I do not see any difference between doing that or having a script that says “if the input box is not empty print the content”. Afterall, unless you do something else otherwise there’s no real difference except that you type and extra 2 lines of code into your script!

You may also like...