How To Get PHP Errors To Display and Report Correctly

Have you come across a problem with your PHP code, and need to fix it? The best way to debug is by turning on and displaying PHP errors. When programming in PHP, eventually you'll need to resort to the error log or display those errors to your browser. Let's find out how you get PHP errors to display.

Getting Errors To Display In PHP

There are two main ways to get PHP errors to display. Firstly in the error_log and to your web browser. The quickest way to debug in a development environment is to your web browser, such as Chrome or Firefox.

Enable PHP errors in php.ini

If you have access and write permission to your php.ini file, you can add the following PHP directives to your file.

error_reporting = E_ALL
display_errors = On
display_startup_errors = On

error_reporting

Within PHP we can configure the verboseness of the error reporting. This means we can see nothing, everything, or a mixture between the two. There are many reporting levels, from deprecated, warnings, to notices. As outlined in the predefined error handling constants, we can provide a value number or a PHP constant. In our example above we're using E_ALL value (32767), which displays all types of errors, warnings, and notices. Such warnings could include fatal errors and warning: Undefined array key.

display_errors

Once we've set the level of error reporting we want in our application, we need to turn on the runtime configuration, display_errors. This tells PHP that we do want errors to be printed on the screen as part of the output. This option is enabled by default under a default PHP setup but may have been changed by your hosting.

display_startup_errors

If you're debugging, turning it on display_startup_errors can be helpful if an error occurs during PHP's startup sequence.

Enable PHP errors at runtime

If you're looking to debug one script file or don't have access to your php.ini file, setting the error reporting at PHP's runtime is another popular choice for developers. To display the PHP errors you can add the same PHP directives to the top of your PHP file.

<?php
 ini_set('display_errors', '1');
 ini_set('display_startup_errors', '1');
 error_reporting(E_ALL);

Handling PHP errors in production

When running your website in production mode, it's best practice to keep your PHP errors hidden from the end-user. That means instead of displaying them on the front-end with PHP's output the best way is to store them in a log, known as an error log. By default, errors are not logged, therefore setting this configuration option to "1" (true) will enable this. This is in place of display_errors which should be turned off "0" (false).

<?php
 ini_set('log_errors', 1);
 ini_set('display_errors', '0');
 ini_set('display_startup_errors', '0');
 error_reporting(E_ALL);

If you don't want to use PHP's default error log location, you can tell PHP where you want the error logs stored instead, including the file name.

<?php
 ini_set('error_log', '/var/www/logs/error.log');

You can also use PHP's info page to find out all of your runtime configuration options to check you've set the error reporting to your needs.

<?php
 phpinfo();

You can view the PHP info information on the command line as well.

php -i

Senior PHP developer with near two decades of PHP experience. Author of Dev Lateral guides and tools. The complete place for PHP programmers. Available to hire to help you build or maintain your PHP application.

Looking for industry-leading PHP web development?

API development WordPress Hosting ★ and more 🐘

We use cookies to enhance your browsing experience and analyse website traffic in accordance with our Privacy and Cookie Policy. Our cookies, including those provided by third parties, collect anonymous information about website usage and may be used for targeted advertising purposes. By clicking "Reject non-essential" you can opt out of non-essential cookies. By clicking "Accept all" you agree to the use of all cookies.


Reject non-essential Accept all