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.
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: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.
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.
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. |
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]