PHP Error Handling

PHP Error Handling tutorial will explain you What are the different PHP Error Handling methods? with example. PHP Errors are the most common event, a developer faces when programming. PHP Error handling is the process of catching errors raised by your program and then taking appropriate action.

If you would handle errors properly then it may lead to many unforeseen consequences. PHP’s default error handling is quite simple, it just sends error message with filename, line number and a message describing the error handling to the browser.

PHP Error Handling

PHP Error handling methods:

  • PHP die() method
  • setting custom error handler and error triggering
  • error reporting

PHP Error Handling using die() method

die() displays a message (or calls a function) and then stops parsing of the current script. It is most often used for debugging or providing PHP error handling.

Example on die() method:

If fopen failed then message passed to die method will be displayed and it uses a simple PHP error handling mechanism to stop the script after the error.

Defining Custom Error Handling Function:

You can write your own function to handling any error. PHP provides you a framework to define PHP error handling function.
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 on PHP Error Handling:

Parameter Description
error_level Required – Specifies the error report level for the user-defined error. Must be a value number.
error_message Required – Specifies the error message for the user-defined error
error_file Optional – Specifies the filename in which the error occurred
error_line Optional – Specifies the line number in which the error occurred
error_context Optional – Specifies an array containing every variable and their values in use when the error occurred

Possible Error levels

These error report levels are the different types of error the user-defined error handler can be used for.
These values can used in combination using | operator

Value Constant Description
1 E_ERROR Fatal run-time errors. Execution of the script is halted
2 E_WARNING Non-fatal run-time errors. Execution of the script is not halted
4 E_PARSE Compile-time parse errors. Parse errors should only be generated by the parser.
8 E_NOTICE Run-time notices. The script found something that might be an error, but could also happen when running a script normally
16 E_CORE_ERROR Fatal errors that occur during PHP’s initial startup.
32 E_CORE_WARNING Non-fatal run-time errors. This occurs during PHP’s initial startup.
256 E_USER_ERROR Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error()
512 E_USER_WARNING Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error()
1024 E_USER_NOTICE User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error()
2048 E_STRICT Run-time notices. Enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.
4096 E_RECOVERABLE_ERROR Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler())
8191 E_ALL All errors and warnings, except level E_STRICT (E_STRICT will be part of E_ALL as of PHP 6.0)

All the above error level can be set using following PHP built-in library function where level cab be any of the value defined in above table.
int error_reporting ( [int $level] )

Following is the way you can create one error handling function:

Once you define your custom error handler you need to set it using PHP built-in libraryset_error_handler function. Now let’s examine our example by calling a function which does not exist.

Trigger an error

In a script where users can input data it is useful to trigger errors when an illegal input occurs. This can be done by the trigger_error()function.


This produces an “Notice error”.

Example to show the trigger_error():

Example of custom PHP error handling

Eexample of custom PHP error handling:

Output :

Error Logging in PHP

In error logging, according to how the error_log configuration is set in the php.ini file, PHP sends an error log to the servers logging system or a file.
error_log() is a great way to get error data saved to disk (or elsewhere) in just one call.

Syntax for Error Logging in PHP:


string message: Error message
int log_type (optional): Type of logging to use; defaults to syslog (0)
string error_destination (optional): If the logging type is email or a file, the destination address or filename
string additional_headers (optional): If the logging type is email, additional email headers

At its simplest, you can pass error_log() just one parameter – an error message – and it will log it for you. To get that, edit your php.ini file and set the error_log directive to a location Apache/PHP can write to. For example, /var/log/php_error would be good for Unix, and c:/windows/php_error.log is good for Windows.

With that done (don’t forget to restart Apache if necessary!) we can go ahead and use error_log in its most simple form:

That will output data to our error log. It should also output actual execution errors into the file – something like

Parameter two – oddly – takes an integer to determine where your error should be sent: 0 sends it to the error_log (like the default), 1 sends it by email using the mail() function, 3 is unused, and 4 saves it to a file of your choice.

The third parameter qualifies the second in that if you set parameter two to be one (send error by email), parameter three should be the email address of the recipient. Similarly if you set parameter two to be three, parameter three should be the filename to save the error to. There is a slight twist to saving to a custom file, because PHP will not do any of the nice formatting for you like it does in the default error log.

Here’s a Windows example – note the \r\n for end of line:

Note: If you’re running a very new version of PHP (that is, released after PHP 5.0.1) you will have the constant PHP_EOL available to you in your scripts, which is automatically set to either \r, \n, or \r\n depending on what platform the script is running.

Previous articlePHP Secure Email
Next articlePHP Exception Handling
I am a young Information Systems Security Engineer(CEH). I had started with penetration testing when i am 17 years old. It all started with Kali linux and Metasploit.In free time i will play with Security holes/Bug bounty/Pentesting and Hacking !!!