This tutorial contains some of the most common error checking methods in PHP.
We will show different error handling methods:
The die()
function is used to output an error message when an error is encountered.
The system error message might be big and difficult to understand. This error message is suppressed and the message given in the die()
function is displayed.
Let us have an example to demonstrate die()
function.
<!DOCTYPE html>
<html>
<head>
<title>Error Handling in PHP</title>
</head>
<body>
<?php
$ftl=fopen("MY_text.txt","r");
?>
</body>
</html>
To prevent the user from getting an error message like the one above, we test whether the file exist before we try to access it :
<!DOCTYPE html>
<html>
<head>
<title>Error Handling in PHP</title>
</head>
<body>
<?php
if(!file_exists("examples/my-file.txt")) {
die("File not found");
}else {
$file = fopen("examples/my-file.txt","r");
print " This file is Opend sucessfully";
}
// Test of the code here.
?>
</body>
</html>
We can use die()
to handle errors, but we can make the error messages more creative just like the system error messages by providing the type of error, error message, file in which the error occurred by defining a error function.
This function can have in total 5 parameters of which 2 are basic and mandatory and the other 3 are optional.
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):
error_function
(error_level,error_message,error_file,error_line,error_context);
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 file name 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 |
These error report levels are the different types of error the user-defined error handler can be used for.
Constant | Description | Value |
---|---|---|
E_ERROR | Execution of the script comes to a halt. An example is memory allocation problem. | 1 |
E_WARNING | Execution of the script is not halted, warnings generated. | 2 |
E_PARSE | Parse errors generated by parsers during compilation. | 4 |
E_NOTICE | Run-time notices which indicated that may be an error took place but may also be a normal course of action. | 8 |
E_CORE_ERROR | Fatal errors that occur during the initial startup of PHP. | 16 |
E_CORE_WARNING | Warnings (execution of the script is not halted) that occur during PHP's initial startup. | 32 |
E_COMPILE_ERROR | Fatal compile-time errors. | 64 |
E_COMPILE_WARNING | Compile-time warnings, execution of the script is not halted. | 128 |
E_USER_ERROR | User-generated error message. | 256 |
E_USER_WARNING | User-generated warning message. | 512 |
E_USER_NOTICE | Same as E_NOTICE. The only difference is, trigger_error() function is used here to generate the error message. | 1024 |
E_STRICT | User-generated notice message. | 2048 |
E_RECOVERABLE_ERROR | Catchable fatal error. | 4096 |
E_DEPRECATED | Run-time notices. | 8192 |
E_USER_DEPRECATED | User-generated warning message. | 16384 |
E_ALL | All errors and warnings, as supported. Exception level E_STRICT. | 30719 |
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.
To prevent the user from getting an error message like the one above, we test whether the file exist before we try to access it :
<?php
function cus_Error_handler($error_no, $error_str) {
echo "<b>Error:</b> [$error_no] $error_str<br>";
echo "Ending Script";
die();
}
?>
set_error_handler("cus_Error_handler");
<!DOCTYPE html>
<html>
<head>
<title>Error Handler</title>
</head>
<body>
<?php
//error handler function
function cus_Error_handler($error_no, $error_str) {
echo "<b>Error:</b> [$error_no] $error_str<br>";
}
//set error handler
set_error_handler("cus_Error_handler");
//trigger error
echo($my_test);
?>
</body>
</html>