How do I get PHP errors to display

Nadia S.

The Problem

You have errors in your PHP code, but the error message isn’t displaying in the browser. Or maybe you’re debugging your code but you don’t see any error messages in the browser.

The Solution

Browser error messages probably aren’t showing because of one or more display settings. To fix this, you’ll need to change the error display settings in either the PHP configuration file (php.ini) or the server configuration file (.htaccess).

You may not have access to the php.ini file, in which case you can edit the .htaccess file.

Change the php.ini configuration settings

How you change the php.ini settings will depend on whether or not you have write access to the php.ini file.

With php.ini write permission

If you have write permission to the php.ini file, check three key parameters in the php.ini configuration file directly:

error_reporting = E_ALL display_errors = On display_startup_errors = On

On Ubuntu, you can run php --ini to find the location of the file - likely one or two levels around the etc folder. If you’re using the XAMPP GUI, you can find it by clicking on “Volumes”, then “Explore”, and locating the etc folder. If you installed Apache separately, php.ini is located in the /etc/php/<version>/apache directory.

Without php.ini write permission

If you don’t have permission to modify php.ini, you can change the file’s permissions or add the following code to the beginning of the PHP script:

<?php ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); // rest of the code

Restart the web server for the changes to take effect.

Change the .htaccess configuration settings

If you are using Apache, add the following directives to the .htaccess file:

php_flag display_errors on php_flag display_startup_errors on php_value error_reporting E_ALL

The .htaccess file is a hidden website configuration file that allows you to make changes to your website without changing the server configuration settings. It’s usually located in your website root directory (where your main index.html or index.php file is located).

cd /var/www/html/ sudo nano .htaccess

View PHP errors in production

For security reasons, displaying PHP errors in the browser is not recommended, as this exposes sensitive information. You should rather turn off the error display setting and log errors in another file.

Change the display settings in php.ini as follows:

display_errors = Off log_errors = On

Locate the log file to view the logged errors:

<?php phpinfo();

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.