Display All PHP Errors: Basic & Advanced Usage

If you are having problems with your PHP web application and need to display all the errors and warnings, you are in the right place. 

We will explain all the different ways to enable PHP errors and warnings.

Quickly show all PHP errors

The quickest way to display all PHP errors and warnings is to add these lines to your PHP code file:

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

What do these lines of code do exactly?

The ini_set function will try to override the configuration found in your PHP ini file.

The display_errors and display_startup_errors are just two of the directives that are available.

The display_errors directive will determine if the errors will be displayed or hidden to the user.

Usually, the dispay_errors directive should be turned off after development. 

The display_startup_errors, however, is a separate directive because the display_errors doesn’t handle the errors that will be encountered during PHP’s startup sequence. 

Unfortunately, these two directives won’t be able to display parse errors such as missing semicolons or missing curly braces. In this case, the PHP ini configuration must be modified.

Configure PHP.ini to display all errors

If adding some of the PHP code errors doesn’t show in the browser during testing, then the PHP ini configuration has some additional directives to handle this.

display_errors = on

The display_errors directive must be set to “on” in the PHP ini file.

 The PHP ini file can be found in the displayed output of phpinfo() function and is labeled loaded configuration file.

Display PHP errors via .htaccess configuration

Developers usually have access to the directory files. The directive for showing PHP errors can also be enabled or disabled using the .htaccess file located in the root or public directory of the project.

php_flag display_startup_errors on
php_flag display_errors on

Similar to what will be added to the PHP code to show PHP errors, .htaccess also has directives for display_startup_errors and display_errors. The advantage of showing or disabling error messages in this manner is that development and production can have different .htaccess files, where the production suppresses the displaying of errors.

In the .htaccess file, a custom error log can also be enabled as long as the log folder or the log file is writable by the web server. The log file can be a relative path to where the .htaccess is located, or it can be an absolute path such as /var/www/html/website/public/logs.

php_value error_log logs/all_errors.log

Enable detailed warnings and notices

For a developer, showing warnings and hiding errors is just as simple as adding a single line of code.

error_reporting(E_WARNING);

Leave a Comment

Your email address will not be published. Required fields are marked *