Showing Errors in PHP [Tutorial]

Showing Errors in PHP

This tutorial explains how to use several methods for displaying errors in a PHP application for debugging purposes.

Be Careful

There are security considerations when displaying errors in any application.

Be careful when and where errors are displayed – especially when building products for the web – as the errors may contain sensitive information.

For example, if an application connects to a database and the database crashes, an error will be produced. That error may contain the username and password used to connect to the database.

If these errors are displayed publicly, you’ve just given away access to anyone who wants it!

This is an easy mistake to make and is a common way for bad actors to get ahold of database credentials or user login details – or other data that the application may have processed.

Globally via php.ini

php.ini is the PHP configuration file for an environment. There may be several php.ini files on your system – one for each environment.

Run the following to find out where they are :

php --ini

It is common for there to be a configuration file for both the CLI (Command Line Interface) and a separate configuration file used by the webserver serving PHP applications (e.g., Apache).

Add (or change!) the following values in php.ini:

display_errors = on
display_startup_errors = on
error_reporting = E_ALL

The first line tells PHP to display any errors reported. If display_errors is not enabled, PHP will not display any errors regardless of which errors are reported.

The second line tells PHP to display errors generated not by your code but during the PHP startup process. It should really only be enabled as a last resort, so leave it out if you haven’t exhausted all other options.

The third line tells PHP to report all errors.

Looking to set up PHP and Apache? Check out our article.

Displaying Errors for a Specific Script

It is also possible to display PHP errors only for a specific script or file by adding the following to the beginning of the code:

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

This temporarily sets PHP configuration options to be applied only for the execution of a specific script.

Checking for Syntax Errors

To check if a PHP file is valid before running it, test it from the command line using:

php -l index.php

Displaying Errors via .htaccess

If you are hosting multiple applications configured using .htaccess files, you can set PHP configuration for them separately by adding the following lines to the .htaccess file for each application:

php_flag display_startup_errors on
php_flag display_errors on
php_value error_reporting -1

Logging Errors

By default, PHP errors will be logged by your web server. The location will vary depending on your operating system and web server choice, but usually, they’ll be located at:


…in a subdirectory for your web server or for the PHP CLI.

Changing Log File Location via .htaccess

To change the log file location using .htaccess configuration files on your web server, add the following lines:

php_value error_log path/to/logs/all_php_errors.log

If you use vhosts instead of .htaccess files to configure your web application, you can add these lines to those instead.

Using try/catch Statements to Show Specific Errors

To show the errors for a specific line or block of code, you can wrap it in a try/catch statement and print the errors on exception:

try {
    // Code with errors
catch (Throwable $e) {

Here’s our full article on using the try/catch statement in PHP.


Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

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