Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Custom Error Handler

Custom Error Handler

How to create a custom error handler in PHP

Have you ever wanted to create a custom error handler in PHP to replace the standard output to the browser? Well this is how you do it.

First you set up the error handler to replace the normal one, and set the ini values.

// Change defaults
ini_set('display_errors', 'Off');
ini_set('log_errors', 'On');
define('ERROR_LOG_PATH', 'error_log.txt'); // The path to the error log file

// set to the user defined error handler
set_error_handler("custom_err_handler");

Now that the custom function is defined we must then create it.

The function takes 4 arguments.

  • $num: level of the error raised
  • $str: contains the error message
  • $file: filename that the error was raised in
  • $line: line number the error was raised at
function custom_err_handler($num, $str, $file, $line) {
    print "Error Handler Called!";
    $err = "";
    $err .= "---- PHP Error ----\n";
    $err .= "Number: [" . $num . "]\n";
    $err .= "String: [" . $str . "]\n";
    $err .= "File: [" . $file . "]\n";
    $err .= "Line: [" . $line . "]\n\n";
    error_log($err, 3, ERROR_LOG_PATH);
}

Now to save an error you use "trigger_error" such as in the example.

Here is the complete code.

// set the error reporting level for this script
error_reporting(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);

// Change defaults
ini_set('display_errors', 'Off');
ini_set('log_errors', 'On');

// set to the user defined error handler
set_error_handler("custom_err_handler");

// The path to the error log file
define('ERROR_LOG_PATH', 'error_log.txt');

// Our custom error handler function
function custom_err_handler($num, $str, $file, $line) {
	print "Error Handler Called!";
    global $error_log_path;
    $err = "";
    $err .= "---- PHP Error ----\n";
    $err .= "Number: [" . $num . "]\n";
    $err .= "String: [" . $str . "]\n";
    $err .= "File: [" . $file . "]\n";
    $err .= "Line: [" . $line . "]\n\n";
    error_log($err, 3, ERROR_LOG_PATH);
}

// function to test the error handling
function myFunction() {
	trigger_error("Just a sample errror message");
}

// Call our test function
myFunction();

Here is a snippet from the error file our function writes to

........
---- PHP Error ----
Number: [1024]
String: [Just a sample errror message]
File: [/path/to/file/cause_errors.php]
Line: [20]
........

That is all it takes to set up a custom error handler function.

If you have any questions don't hesitate to ask.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Custom Error Handler

 Jack  Mon Feb 6, 2006 7:00 pm  
So, would you have to define the trigger_error function for everything you use such as..

$query=mysql_query("SELECT * FROM table");
$query2=mysql_num_rows($query);
echo $query2;

$trigger_error($query);

or will it automatticaly detect the errors and log them?
 mkeefe  Mon Feb 6, 2006 9:53 pm  
You would have to use trigger_error if you wanted to use the custom error handler, unless you modify the way PHP interprets errors, which is a bit beyond the scope of this article.

By the way, you would call trigger_error() without the '$', because it is a predefined function within PHP.
 Jack  Wed Feb 8, 2006 8:48 pm  
k, thank you.
 Elliott  Mon Feb 13, 2006 11:15 pm  
$error_log_path should be a constant. There is no need to be importing a global that points to the log file.

define('ERROR_LOG_PATH','error_log.txt');

Other than that, nice :)
 mkeefe  Mon Feb 13, 2006 11:20 pm  
Good idea, addition noted "constant has been added" :)
 roy  Mon Apr 20, 2009 9:09 am  
logging the errors is good idea.

Here is a custom error handler sample, I am trying to handle errors by there types.

http://php-tuts.blogspot.com/2009/04/custom-error-handler-sample-example.html


Thank you for this post. I like the idea of putting the error details in separate txt file.
 Conrad  Fri Jul 24, 2009 5:36 am  
Just to let you know you script is creating the following errors.

I am using PHP Version 5.2.9.

---- PHP Error ----
Number: [8]
String: [Undefined variable: num]
File: index.php]
Line: [6]

---- PHP Error ----
Number: [8]
String: [Undefined variable: str]
File: [index.php]
Line: [6]

---- PHP Error ----
Number: [8]
String: [Undefined variable: file]
File: index.php]
Line: [6]

---- PHP Error ----
Number: [8]
String: [Undefined variable: line]
File: index.php]
Line: [6]
 mkeefe  Mon Aug 3, 2009 10:02 am  
@Conrad - Yes, it seems a few variables should be defined up top. "Undefined Variables" aren't technically an error though.
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles