PHP - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP Error Handling

PHP Error Handling

shape Introduction

In the present chapter of PHP Error Handling, everything related to occurrence of errors in PHP and resolving them will be discussed. As PHP is the most populated Server-side programming language, developer should also know the process of resolving the errors that occur when PHP code is executed. Every PHP application must undergo the process of error handling. But there is no exact method for error handling as it varies from application to application holding the user needs and the style of developer.

PHP Error

The expected or unexpected event that occurs when the PHP code runs is known as an "Error". There may be multiple reasons for the occurrence of errors, for example:
  • When disk space of Web server exceeds.
  • An invalid value is given by the user in a form field.
  • The existence of record is not found in the database.
  • The access to an application is denied to write to a file.
The above given types of errors are known as Runtime errors, as these occur when the script runs. They are different from syntax/compile time errors that are fixed before the code is made to run. To improve the performance of any application it should have the capability to handle runtime errors gracefully i.e. informing the user about the error more clearly.

Error Handling Using die() function

shape Description

While writing PHP program, error conditions are to be checked before moving further and find the correct solution. Below is an example that initially tries to open a text file for reading only. [php] <?php // Try to open a non-existent file $file = fopen("sample.txt", "r"); ?> [/php] When the file is not found in the given location following error occurs: [php]Warning: fopen(sample.txt) [function.fopen]: failed to open stream: No such file or directory in C:\wamp\www\project\test.php on line 2[/php] To avoid these type of errors some measures have to be taken which alerts the users. [php] <?php if(file_exists("sample.txt")) { $file = fopen("sample.txt", "r"); } else { die("Error: The file you are trying to access doesn't exist."); } ?> [/php] Now when the above script you will get the error message like this: [php]Error: The file you are trying to access doesn't exist.[/php] The function die() displays the error messages and terminates the running script if the given file is not found. This is a simple method to check if the file exist or not initially before trying to access it which is quite better to inform the user about the error occurred with ease.

Understanding Error Levels

shape Description

Likewise, there will be many errors occurring that disturbs the functionality of the PHP code. Every error is identified with a integer value. Below are some of the built-in error levels:
Value Error Level Description
1 E_ERROR Once this error occurs, user cannot recover from it. The script execution is terminated immediately.
2 E_WARNING The error occurs at run-time and user can recover from this error. Most of the errors comes under this category only. It will not alter the execution.
8 E_NOTICE It is an indication that something went wrong.
256 E_USER_ERROR A fatal user-generated error message. This is like an E_ERROR, except it is generated by the PHP script using the function trigger_error() rather than the PHP engine.
512 E_USER_WARNING This is similar to E_WARNING but generated by PHP function trigger_error() rather than the PHP engine.
1024 E_USER_NOTICE A notice message created by the user similar to E_NOTICE, but created by PHP script with the function trigger_error() instead of PHP engine.
2048 E_STRICT Not exactly an error, but can be occured when PHP finds code that may lead to problems or forward incompatibilities.
8191 E_ALL All errors and warnings, except of E_STRICT prior to PHP 5.4.0.

Creating a Custom Error Handler

shape Description

The developer can create their own error handler functions to handle the run-time errors generated by PHP engine. The Custom Error Handler helps in this process by providing high flexibility and great control over the errors. The handler can identify the error easily and finds a better solution for it whether to display a message to the user, log the error in a file or database or send by e-mail, attempt to fix the problem and carry on, exit the execution of the script or ignore the error altogether. Atleast two parameters (errno and errstr), must be accepted by the Error Handler. However, it can optionally accept an additional three parameters (errfile, errline, and errcontext), as described below:
Parameter Description
errno Required - Defines error level, as an integer
errstr Required - Defines the error message as a string
errfile Optional - Defines script filename where the occurrence of error is seen
errstr Optional - Defines the line number on which the error occurred, as a string
errno Optional - Defines array having variables and their values that existed at the time the error occurred.
Below is an example for Custom PHP Error Handling function, customError() which triggers whenever an error occurred and outputs the error details onto the browser and alters the script execution. [php] <?php // Error handler function function customError($errno, $errstr){ echo "<b>Error:</b> [$errno] $errstr"; } ?> [/php]


shape Key Points

  • PHP Error Handling function die() displays the error messages and terminates the running script if the given file is not found.
  • ERROR and WARNING are the important levels of PHP Errors.
  • The developer can create their own error handler functions to handle the run-time errors generated by PHP engine.