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:

 
 

Want to be a PHP master?
Subscribe to our newsletter and download the PHP programming Cookbook right now!
In order to get you familiarized with PHP development, we have compiled a comprehensive guide about all major aspects of PHP programming. The perfect tool to boost your WEB development career. Besides reading them online you may download the eBook in PDF format!
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

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:

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:

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
Exit mobile version