Skip to content

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 error add lines

php
<?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 directive, 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 php

ini
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 located 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 errors

apache
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, provided the log file or directory is writable by the web server. The log file can be a path relative to where the .htaccess file is placed, or it may be an absolute path like /var/www/html/website/public/logs. To configure this, add the following directives:

apache
php_value log_errors 1
php_value error_log /var/www/html/website/public/logs/php_errors.log

Note: These directives only work when PHP runs as an Apache module (mod_php). They are ineffective with PHP-FPM or CGI setups.

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:

Displaying Fatal Errors in PHP

php
<?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.

Dual-run preview — compare with live Symfony routes.