PHP error handling


Does this type of error handling code look familiar?

function doFunction($var) {
        if(is_numeric($var)) {
                /* do some stuff*/
        } else {
                return -1;
        }
}

BLEH. How ugly is that? There’s no indication whether -1 is actually an error or a valid return value, or what it means. And other functions might use false to indicate errors so there’s inconsistency. So I’ve written a very simple function to help you give meaningful PHP error messages.

Now, PHP already comes with a built-in function for reporting errors, called “trigger_error”. But trigger_error always reports the line and file that trigger_error was called on. Which isn’t very useful.

For example:

main.php:

include('functions.php');
$x = 'test';
doFunction($x); // line 3

functions.php:

function doFunction($var) {
        if(is_numeric($var)) {
                /* do some stuff*/
        } else {
                trigger_error('var must be numeric'); // line 5
        }
}

Because we’re using trigger_error, this will output something like this:

Notice: var must be numeric in functions.php on line 5

Which isn’t very useful, because really we’d want to know that main.php:3 was where the error was actually caused.

We’d really rather see something like this:

Notice: var must be numeric in doFunction called from main.php on line 3

Here’s a function to do just that:

function error($message, $level=E_USER_NOTICE) {
        $caller = next(debug_backtrace());
        trigger_error($message.' in '.$caller['function'].' called from '.$caller['file'].' on line '.$caller['line'].''."\n
error handler", $level); }

So now in our example:

main.php:

include('functions.php');
$x = 'test';
doFunction($x); // line 3

functions.php:

function doFunction($var) {
        if(is_numeric($var)) {
                /* do some stuff*/
        } else {
                error('var must be numeric'); // line 5
        }
}

function error($message, $level=E_USER_NOTICE) {
        $caller = next(debug_backtrace());
        trigger_error($message.' in '.$caller['function'].' called from '.$caller['file'].' on line '.$caller['line'].''."\n
error handler", $level); // line 11 }

now outputs:

Notice: var must be numeric in doFunction called from main.php on line 3
error handler in functions.php on line 11

Isn’t that much nicer. Why yes, yes it is!

By the way, if you’re having trouble getting PHP to display error messages, try setting your php.ini’s error_reporting to E_STRICT (which you should be trying to adhere to anyway), or change $level to E_USER_WARNING

You can also use the E_USER_ERROR constant to halt the PHP interpreter.


Related Posts:

, , , , ,

  1. #1 by Del on July 5, 2009 - 4:31 am

    Yeah, i have used this method before by calling the debug_backtrace() function, my implementation wasn’t as clean as yours but it essentially did the same thing.

    Very useful and a great article … Keep them coming!

    • #2 by user24 on July 5, 2009 - 8:30 am

      cheers ;0) Yeah I did write a little thing to make debug_backtrace prettier at one point too, so you effectively step through the whole execution path and dump out the function, file and line number etc. But I found this more useful in general.

Comments are closed.