PHP error handling

In this lecture we will discuss most common errors in PHP script and also PHP messages about them. First, we will talk how to hide these error messages from users, and then we will talk about the errors themselves.

Error types

All programming error are usually divided into three groups:

In addition to these, we have to add environmental errors, which are not due to the any fault of the programmer, but the result of the environmental factors beyond the programmer's control. Even if we ensure that the ode is bug-free, that doesn't guarantee that it will be necessarily run without errors. Unfortunately, programmers often have to rely on factors beyond their control (such as included files, for example).

As you can probably guess we are most interested in runtime errors. Definitely, we have to eliminate all syntax errors because our scripts won't work with them and of course we do not need to have any logical error, but this is more of a dream, than a goal. Thus, we need to deal mostly with runtime errors. That is, situations when script works (has no or syntax errors), but cannot perform its functions and interrupts with an unpleasant error.

PHP error messages

PHP error messages provide us with a lot of information in the following format:
Error level: Error message in filename on line #
Here is an example:
Warning: fopen("abcdef.test", "r") - No such file or directory in /somedir/somesubdir/script.php on line 10
There are four basic error levels in PHP. There are: Parse errors obviously are not of concern of ours because we assume that we successfully corrected all syntax errors. Notices by default are not displayed. That is, we usually need to do some steps to see them. It's not a bad idea when we debug scripts on a special separate design web server, but this option is very rare turned on or real web sites. This left us to deal with fatal errors and warning, which are actually also errors.

Setting the error reporting level

Usually it is a very good idea to suppress error messages because these messages contains many information about our site structure or content of the script. These is obviously not give away information. That is we would like to prevent users from seeing these messages. There is a special function error_reporting() that controls which the levels of errors will be displayed. The general syntax of the function is:
integer error_reporting(level);
This function takes only one argument the level of errors PHP reports. The possible arguments are in the table below:
Value Description
E_ALL All errors and warnings
E_ERROR fatal run-time errors
E_WARNING run-time warnings (non-fatal errors)
E_PARSE compile-time parse errors
E_NOTICE run-time notices (these are warnings which often result from a bug in your code, but it's possible that it was intentional (e.g., using an uninitialized variable and relying on the fact it's automatically initialized to an empty string)
E_CORE_ERROR fatal errors that occur during PHP's initial startup
E_CORE_WARNING warnings (non-fatal errors) that occur during PHP's initial startup
E_COMPILE_ERROR fatal compile-time errors
E_COMPILE_WARNING compile-time warnings (non-fatal errors)
E_USER_ERROR user-generated error message
E_USER_WARNING user-generated warning message
E_USER_NOTICE user-generated notice message

Using this function we can get rid of annoying warnings about uninitalized variables and also hide inner structure from users by preventing displaying error messages like "Warning: file 'data/logins.txt' not found.". If argument level is set to zero, it ensures that no PHP error/warning messages will be invoked.

There is another way to suppress an error message generated by a function. All we need to do is to use @ sign in front of the function name. For example, if we do not want to see an error message about a missing file we can use code like this:

$fp = @fopen("test.txt", "r");
Please notice that this sign suppresses only this function error message.

Error handling

Now we know how to suppress the error messages, but we still need to do something about errors themselves. Most functions in PHP return 0 or an empty string in case of error. There are some that return other values if an error occurred. For details see function documentation. That gives us an opportunity to handle the error by ourselves: Where print_error_message() is a function that prints our own error message. This function may look like:
function print_error_message($msg)
{
    echo "<center>".
         "<div style='color:red; background-color:lightblue; border-style:double; 
                      text-align:left; font-size:130%; padding:1px;'>".
         "Sorry $msg </div></center>";
    die("</body></html>\n");
}

Logging errors

PHP provides a handy error_log() function for logging any errors which occur. This function can take up to four parameters but required only two:
error_log(message, type[, destination, extra_headers]);
The first argument is the error message to be logged. The output can be directed to one of four different targets:
  • PHP's system error log (type = 0)
  • e-mail address supplied by the third parameter (type = 1)
  • debugging connection (type=2) not available in PHP v4
  • file specified by the third argument (type=3)

    In order to set PHP's system log file you need to edit php.ini file. Set the following two variables:

    log_errors = On
    error_log = c:\temp\phperror.log
    
    The first line makes the PHP log errors and the second assigns a file for the error messages. You can decide to keep PHP error log together with Apache log to do that set the second line to be
    error_log = syslog
    

    The following example shows how to hide a real error message from user, print a nice looking message instead, and save real error report to a log file:

    Please consult official documentation for more details.