How to Display PHP Errors

Different errors can occur during the execution of a PHP application. Developers often meet obstacles while trying to display such errors. If you also have issues with your PHP application and want to display the errors, you are in the right place.

Watch a course Learn object oriented PHP

The Quick Display of a PHP Error with ini_set

If you want to quickly show all the PHP errors and warnings, then you should add the following lines to your code:

<?php

 ini_set('display_errors', 1);
 ini_set('display_startup_errors', 1);
 error_reporting(E_ALL);

?>

The ini_set function may allow overriding the configuration detected in the PHP ini file. Different directives can be available. Two of them are display_errors and display_startup_errors that are demonstrated in the example above. The first director, as a rule, determines whether the errors are going to be shown to the user or hidden. After development, it should be turned off.

The display_startup_errors directive is considered a separate one, as the display_errors doesn’t deal with the errors occurring during the startup sequence.

It is essential to note that the directives above can’t parse errors like missing curly braces or missing semicolons. In such cases, it is necessary to modify the PHP ini configuration.

Using Configuration of PHP.ini

The PHP.ini configuration has additional directives aimed at handling and showing the errors in the browser during testing.

To use it, you need to act like this:

display_errors = on

So, you need to turn on the display_errors directive inside the PHP.ini file. It can display all the errors, particularly, parse and syntax errors that can’t be shown by calling only the ini_set function. That file can be detected in the displayed output of the phpinfo() function. When the web application is in production, this directive should be set to off.

Displaying Errors with the .htaccess Configuration

With the .htaccess file, a directive for displaying errors can be enabled or disabled like this:

php_flag display_startup_errors on
php_flag display_errors on

The .htaccess file also encompasses directives such as display_startup_errors and display_errors.

Inside this file, a custom error log may be enabled as far as the log file or the log folder is writable by the web. The log file can be a path to the place .htaccess is placed. Either it may be an absolute path like /var/www/html/website/public/logs:

Displaying Fatal Errors

If you only want to display fatal errors and not warnings or notices, you can modify the error reporting level to exclude non-fatal errors. Here's an example:

<?php

error_reporting(E_ERROR);
ini_set('display_errors', 1);

In this example, the error_reporting function is set to only report fatal errors, using the E_ERROR constant. This constant represents the most severe type of error in PHP and includes fatal errors, which halt the script execution, but not warnings or notices.

The ini_set function is used to turn on the display of errors, just as in the previous example. This will display any fatal errors that occur in your PHP code on the web page.

Note that this approach will only display fatal errors and suppress all other errors, including warnings and notices. This can be useful in some cases, but it may also mask other issues that could be important for debugging and troubleshooting your code. It's generally recommended to fix all errors and warnings, and to use a comprehensive error reporting system that logs errors and sends notifications to developers or administrators.