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.

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: