The default error handling in PHP is very simple. An error message with filename, line number and a message describing the error is sent to the browser.
----------------------------------------------------------------------------------------------------------------------------------
PHP Error Handling
When creating scripts and web applications, error handling is an important part. If your code lacks error checking code, your program may look very unprofessional and you may be open to security risks.
I will show different error handling methods:
- Simple "die()" statements.
- Custom errors and error triggers.
- Error reporting.
Basic Error Handling: Using the die() function
If the file does not exist you might get an error like this:
############################################################
Warning: fopen(welcome.txt) [function.fopen]: failed to open stream: No such file or directory in C:\webfolder\test.php on line 2
############################################################
To avoid that the user gets an error message like the one above, we test if the file exist before we try to access it:
Now if the file does not exist you get an error like this:
############################################################
File not found
############################################################
The code above is more efficient than the earlier code, because it uses a simple error handling mechanism to stop the script after the error.
However, simply stopping the script is not always the right way to go. Let's take a look at alternative PHP functions for handling errors.
Creating a Custom Error Handler
This function must be able to handle a minimum of two parameters (error level and error message) but can accept up to five parameters (optionally: file, line-number, and the error context):
Syntax: error_function(error_level,error_message, error_file,error_line,error_context)
Error Report levels
These error report levels are the different types of error the user-defined error handler can be used for:
Now lets create a function to handle errors:
The code above is a simple error handling function. When it is triggered, it gets the error level and an error message. It then outputs the error level and message and terminates the script.
Set Error Handler
The default error handler for PHP is the built in error handler. We are going to make the function above the default error handler for the duration of the script.
set_error_handler("customError");
Since we want our custom function to handle all errors, the set_error_handler() only needed one parameter, a second parameter could be added to specify an error level. For instances:
############################################################
The Output:
Error: [8] Undefined variable: test
############################################################
Trigger an Error
In a script where users can input data it is useful to trigger errors when an illegal input occurs. In PHP, this is done by the trigger_error() function. For instances:
############################################################
The Output:
Notice: Value must be 1 or below in C:\webfolder\test.php on line 6
############################################################
An error can be triggered anywhere you wish in a script, and by adding a second parameter, you can specify what error level is triggered.
Possible error types:
- E_USER_ERROR - Fatal user-generated run-time error. Errors that can not be recovered from. Execution of the script is halted.
- E_USER_WARNING - Non-fatal user-generated run-time warning. Execution of the script is not halted.
- E_USER_NOTICE - Default. User-generated run-time notice. The script found something that might be an error, but could also happen when running a script normally.
############################################################
The Output:
Error: [512] Value must be 1 or below
Ending Script
############################################################
Now that we have learned to create our own errors and how to trigger them, lets take a look at error logging.
Error Logging
By default, PHP sends an error log to the servers logging system or a file, depending on how the error_log configuration is set in the php.ini file. By using the error_log() function you can send error logs to a specified file or a remote destination.
Sending errors messages to yourself by e-mail can be a good way of getting notified of specific errors.
Send an Error Message by E-mail
############################################################
The Output:
Error: [512] Value must be 1 or below
Webmaster has been notified
############################################################
And the mail received from the code above looks like this:
############################################################
Error: [512] Value must be 1 or below
############################################################
This should not be used with all errors. Regular errors should be logged on the server using the default PHP logging system.
.jpg)

