PHP

PHP Show/Display Errors Example

PHP gives the possibility to configure how and which errors are displayed; as it’s an interpreted language, it’s not as strict as compiled languages. In this example we will see how we can configure PHP for that.

For this example, we will use:

  • Ubuntu (14.04) as Operating System.
  • Apache HTTP server (2.4.7).
  • PHP (5.5.9).

 
 

Tip
You may skip environment configuration and jump directly to the beginning of the example below.

1. Preparing the environment

Below, commands to install Apache and PHP are shown:

sudo apt-get update
sudo apt-get install apache2 php5 libapache2-mod-php5
sudo service apache2 restart

2. Different error levels in PHP

PHP has the following error levels, all of them generated at runtime:

  • Notices: like small errors. They won’t stop the script execution, but they may be an error source in other parts of the program.
  • Warnings: more serious errors, but that won’t neither stop the execution. Actually, the only difference with notices is the interpretation that PHP makes of the gravity of the error.
  • Execution errors: fatal errors, such as calling non-existing functions, that will cause the script execution end.
  • Parse errors: language syntax errors, like forgetting a semicolon after a statement.

The classification PHP makes of notices and warnings is quite odd, since they don’t stop the script execution, but they both can be the source of a more serious error.

There are also other errors, such us for notifying about the use of deprecated built-in elements, that will be removed from future PHP versions; or for suggesting changes in code for its better fitting for future versions.

3. Configuring the errors displayed

The configuration of the errors displayed by PHP is made in its configuration file, in /etc/php5/apache2/php.ini.

First of all, we have to ensure that the errors will be displayed, with the following directive:

display_errors = On

The directive for configuring the reporting error levels is error_reporting. To show every error, we would set as:

error_reporting = E_ALL

3.1. Combining displaying error levels

Even if it is not usual, is possible to combine error types.

Actually, the error directive is expecting a bitmask. So, for combining different error types, we have to use the bitwise operators: & (and), | (or), ~ (not), and ^ (xor, exclusive or).

For example, for showing only notices and warnings, we have to set:

error_reporting = E_NOTICE | E_WARNING

Or, for showing every error but ignoring the notices:

error_reporting = E_ALL ^ E_NOTICE

4. Catching these errors in PHP code

You probably have ever asked yourself why isn’t it possible to handle that errors in the code, as exceptions in a try-catch.

This is possible, setting a custom error handler, that will be able to handle these notices, warnings and errors in the code, which may result useful.

For that, first, we need to define a function for the handling:

custom_error_handler.php

<?php

function nonFatalErrorHandler($errorNumber, $errorMessage, $errorFile, $errorLine) {
    echo "Catching a non fatal '$errorNumber' error '$errorMessage'in $errorFile file, "
        . "in line $errorLine.<br>";
}

function fatalErrorHandler() {
    $error = error_get_last();

    $errorNumber = $error['type'];
    $errorMessage = $error['message'];
    $errorFile = $error['file'];
    $errorLine = $error['line'];

    echo "Catching a fatal '$errorNumber' error '$errorMessage' in $errorFile file, "
        . "in line $errorLine.<br>";
}

The non-fatal and fatal errors are handled differently, so we coded two functions, in lines 3 and 8.

Then, we have to set these handlers:

naive_script.php

<?php

require_once('custom_error_handler.php');

set_error_handler('nonFatalErrorHandler');
register_shutdown_function('fatalErrorHandler');

echo $undefined;
require('undefined.php');
undefined();

In lines 5 and 6, we pass the defined functions in previous file as callbacks, for each error type. After, the script will generate different types of errors, to test that our handlers are working.

Note: if PHP is configured to display errors, it will display the fatal ones as it does by default, even if we have defined a custom handler.

5. How should you configure the error displaying?

The general rule is:

  • For development environments, display every error. That will help us to write code that fits with PHP rules.
  • For production environments, don’t display any error. The errors can show information that the user shouldn’t know, such us server paths, files, Operating System that can be deduced from the paths, or any other information that shouldn’t be displayed.

6. Download the source code

This was an example of error displaying in PHP.

Download
You can download the full source code of this example here: PHPShow-DisplayErrorsExample
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button