Easily show all errors in PHP

Easily show all errors in PHP

Published
Oct 7, 2023
Written by
Benjamin Crozat
0
comments
3 minutes
read

Show all PHP errors using ini_set() and error_reporting()

To showcase all errors in PHP, include these lines at the top of your PHP file:

ini_set('display_errors', 1);

ini_set('display_startup_errors', 1);

error_reporting(E_ALL);

By tweaking these settings using the ini_set() and error_reporting() functions, PHP will now show all runtime errors, startup errors, and even notify you about potential issues.

Let’s decode what all this means:

  • display_errors: This key is a directive which determines whether errors should be printed as a part of the program’s output or hidden. We set it to 1 to instruct PHP to display errors.

  • display_startup_errors: This directive decides if PHP will show errors that occur during PHP’s startup sequence. We set display_startup_errors to 1, so PHP clearly communicates any startup issues.

  • error_reporting(E_ALL): This setting controls the level of error reporting. E_ALL is a constant that instructs PHP to show all possible errors, warnings, and notices.

How to adjust the setting in your php.ini to show all errors

Besides adding these values in your PHP script, you can also set them globally from the php.ini file. Here are the steps to find and edit this file:

  1. Open a command line, and run php --ini. If PHP is installed correctly, this command will output the location of the php.ini file.
  2. Open php.ini in your preferred text editor. Scroll through the file or do a quick search for display_errors, display_startup_errors, and error_reporting.
  3. Set the values as we did in our script - display_errors and display_startup_errors to 1, and error_reporting to E_ALL.

Remember to save your changes. And if you don’t want to shout at your computer because nothing is happening, restart your PHP server to apply them.

Don’t display all PHP errors in production

Altering these settings can be very useful for debugging during development, but beware that you must not do it in the context of a live application.

Having PHP show all errors can give malicious users more information about your server configuration and potential attack routes.

Therefore, always ensure production-safe settings values before deploying. As a safer alternative in production, consider logging errors to a non-publicly accessible file that you can review later. Set log_errors = on and provide a file path for error_log in the php.ini file for this purpose.

About Benjamin Crozat
Benjamin Crozat

Hi! I’m from the South of France and I’ve been a self-taught web developer since 2006. When I started learning PHP and JavaScript, PHP 4 was still widely used, Internet Explorer 6 ruled the world, and we used DHTML to add falling snow on websites.

Being able to educate myself for free on the web changed my life for the better. Giving back to the community was a natural direction in my career and I truly enjoy it.

Therefore, I decided to take action:

  1. I launched this blog in September 2022 with the goal to be in everyone’s Google search. I get more than tens of thousands of monthly clicks from it and even more visits overall (my analytics dashboard is public by the way).
  2. I also started growing my X (formerly Twitter) account at the same time, which has now over 7,000 followers.
  3. All the content I write is free thanks to my sponsors.

I also want to be completely free with my time and make a living with my own products. In April 2024, I launched Nobinge, a tool to summarize and chat with your content, including YouTube videos.

Believe me, I’m just getting started!

0 comments

You need to be signed in to comment this post.
Sign in with GitHub