Error Handling in PHP
Error handling is the process of catching errors from program and then taking appropriate action. Before proceeding for error handling you should get to know how many types of errors occurred in PHP.
Notices: These are non-critical errors that occurred while executing a script – for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all – although, as you will see, you can change this default behavior.
Warnings: These are more serious errors – for example, attempting to use a file (using include() method) which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.
Fatal Errors: These are critical errors – for example, creating an object of a non-existent class, or calling a function which doesn’t exist in your program. These errors cause the immediate termination of the script, and PHP‟s default behavior is to display them to the user when they take place.
It’s very simple in PHP to handle errors.
Using die() function
While writing your PHP program you should check all possible error condition before going ahead and take appropriate action when required.
die(“File not found”);
$file = fopen(“/docs/resume.txt”,”r”);
$a = 0;
$b = 200;
$result = $b/$a;
echo “RESULT: “. $result;
Output: Warning: Division by zero in … on line 4
echo “Hi lets learn PHP”
echo “<br/>This is another line”;
Output: Parse error: syntax error, unexpected ‘echo’ (T_ECHO), expecting ‘,’ or ‘;’ in … on line 3
Some possible errors in PHP
E_ERROR – Fatal Run-time errors. Execution of the script is halted
E_WARNING – Non-fatal errors. Execution is not halted
E_PARSE – Compile-time parse errors.
E_NOTICE – Run-time notices.
E_ALL – All errors and warnings, except level E_STRICT
E_CORE_WARNING – Non-fatal run-time errors. This occurs during PHP’s initial start-up.
There are some different locations where we can control errors:
In the php.ini file
In the .htaccess file on your web server
From your own PHP code.
We could do the same from our PHP code during runtime by calling the error_reporting() function as below:
To turn on error logging and log the errors to our specific log file (instead of the default error log file, which is often the web server error log file)
log_errors = on
error_log = “/tmp/error.log”
Creating a Custom Error Handler:
We simply create a special function that can be called when an error occurs in PHP. In the example we will send an e-mail with an error message, if a specific error occurs:
//error handler function
function customError($errno, $errstr)
echo “<b>Error:</b> [$errno] $errstr<br>”;
echo “Error Occured”;
error_log(“Error: [$errno] $errstr”,1,
//set error handler
trigger_error(“Value must be 1 or below”,E_USER_WARNING);
set_error_handler(): The default error handler for PHP is the built in error handler. We have made the function above the default error handler for the duration of the script. 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.
trigger_error() function will raise error when an illegal input occurs.
Using Include() Function:
Suppose that you want to print the same message on every page like your company details and address, or maybe today’s date. If you are placing the same coding on each page of your site that is not very efficient because-
– You are typing the same information over and over again, which is not the good approach.
– In the case of an update or a change, you have to make the change in every single page of your site which is very time consuming process.
A solution to this problem is to use an include() method. You take commonly used information and put it in a separate file. For example, if you have a set of variables that need to be use in every page on your site, you could define them once, in a single PHP file. Then, on each of your pages, you can include() it by referencing that file.
Difference between Include and require:
The main difference between include and require statements in PHP is that if any error occurred include will raise an error message and then will continue the rest of the code placed in the page where as require will raise a fetal error and terminates execution.
Function require () throws an error (E_COMPILE_ERROR) and stop the script.
Function include () throws a warning (E_WARNING) but the script will continue.
function require_once is useful if the result of the code is necessary but subsequent inclusions would throw an error. That goes for scripts with functions for instance. If such a script would be included more than once an error would be thrown since functions cannot be redeclared.
Function include_once is useful in the case of including remote files and not wanting them to be included several times due to an HTTP overhead. This is also usefull in payment gateway and process on e-commerce websites.
<title>My Web page</title>
Include_once “menu.php “;
Include_once “menu.php “;
In this example you will see a warning when you attempt to recall file “menu.php”