The following example is a very common instance of an error message when something goes wrong
Fatal error: Call to undefined function hello_world() in C:\local\myphp.php on line 1
The error displayed above isn’t terribly pretty to look at. In fact, if a visitor saw that on your site they’d wonder why it was there in the first place, and would probably be discouraged from exploring the site any more, even if the error wasn’t a fatal error, and they could still browse.
A good way to work around this is to use a custom error handler, which allows you to intercept PHP’s error commands, and replace them with your own, nicely formatted ones, even hiding them away completely from visitors, and displaying them only to yourself.
function custom_error_function($level, $message, $file, $line, $context){ ...
Creating a custom error handler is as simple as creating a new function with up to 5 seperate arguments, defined as follows
- Error level (required) : This specifies the specific level of error reported, useful for determining how serious a fault is
- Error message (required) : This variable holds the error message generated by PHP
- Error file (optional) : This tells the function which file the error occured in
- Error line (optional) : This tells the function which line of the file the error occured on
- Error context (optional) : This holds an array containing all variables in use at the time the fault occurred
The specific error levels generated by an error are outlined below. These are the values passed into the first variable of the custom error handler, along with their associated numerical value
- E_WARNING (2) : Non-fatal, the script can continue
- E_NOTICE (8) : This might be an error, but could also be a normal part of the script, and the code can continue execution
- E_USER_ERROR (256) : A fatal error, triggered by the user. Later, we will see how errors like this can occur. Wxecution of the script will halt
- E_USER_WARNING (512) : A non-fatal error (the code will continue to execute) triggered by the user
- E_USER_NOTICE(1024) : Again, triggered by the user. Non-fatal
- E_RECOVERABLE_ERROR : A fatal error, but one which can be caught, and recovered from, by use of the try…catch statement, which we will cover later in this tutorial
Lets try creating our own customised error function, to try and make the default error message a little bit friendlier to look at
function customError($level, $message){ //note that the final three arguments are optional echo "An error has occured! The error level is $level and the message was '$message'<br/>"; echo "Please get in touch with the site administrator at admin@email.com to notify them of this fault"; }
The function above is still only just that, a function, but when set to be the default error handler for PHP, as the following code will do, it prints out a much friendlier error message, and lets the user know of a course of action they can take to get the problem remedied.
//this function will set the custom error handler to our function above set_error_handler("customError"); //now, let's try triggering that error again... echo ($hello_world); /* our undefined variable */
Which produces
An error has occured! The error level is 8 and the message was 'Undefined variable: hello_world' Please get in touch with the site administrator at admin@email.com to notify them of this fault
A somewhat friendlier thing to see, but still not the best solution. Ideally, we’d prefer for the visitor never to see these error messages, as they indicate that something has gone wrong with the code, reducing their confidence in the site.



