Display All PHP Errors: Basic & Advanced Usage

Display All PHP Errors: Basic & Advanced Usage

A PHP application might produce many different levels of warnings and errors during its execution. Seeing these errors is crucial for developers when troubleshooting a misbehaving application. However, many developers often encounter difficulties displaying errors in their PHP applications, leading to silent app failures.

Understanding the importance of displaying errors in PHP can’t be overstated. Error messages provide vital information about issues that occur while running a PHP script. They inform developers about syntax errors, which are mistakes made while writing code. They also notify developers about missing semicolons, which are important punctuation marks in certain programming languages. Furthermore, they bring attention to more complex issues that need to be debugged. Identifying and fixing these problems becomes a complicated guessing game without visible errors.

With the complexity of PHP web applications, there are multiple methods to display errors, each with advantages depending on your specific use case.

Four different ways to display errors in PHP

Before diving into the details of managing and logging errors in PHP, let’s outline the four main ways to display errors in PHP:

  1. error_reporting: This function allows developers to determine which errors PHP will suggest. It can be set to display all errors, warnings, and notices except for some exceptions like E_NOTICE and E_STRICT.
  2. display_errors: This is a directive in the php.ini file that determines whether errors should be printed to the screen as part of the output or if they should be hidden.
  3. log_errors: This directive tells PHP whether it should log errors. If set to “On”, it will log errors according to what is set in the error_log directive.
  4. error_log string: This directive determines where the error message should be sent. It could be sent to the server’s error log, a TCP port, or a separate file specified in the directive.

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. In this tutorial, we will explain all the different ways to enable PHP errors and warnings. We will also discuss how to write the errors to a log file and even collect them via the Retrace’s error tracking feature.

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);

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

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 from 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 don’t handle the errors that will be encountered during PHP’s startup sequence. The list of directives that can be overridden by the ini_set function is found in the official documentation.

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. This will display all the errors including syntax or parse errors that cannot be displayed by just calling the ini_set function in the PHP code. The PHP ini file can be found in the displayed output of phpinfo() function and is labeled loaded configuration file. This directive in the ini configuration must be set to off, if the web application is in production.

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.
Depending on which files you have access to and how you do deployments and server configurations, you may want to configure display_errors in .htaccess or your PHP.ini file. Many hosting providers will not allow you to modify your PHP.ini file to enable display_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

Sometimes, the warnings that seem not to affect the application at the start will cause some fatal errors in certain conditions. These warnings must be fixed because this means that the application won’t run normally under certain scenarios. In case these warnings cause a lot of errors, then it would be more practical to hide the errors and just show what the warning messages are.

error_reporting(E_WARNING);

For a developer, showing warnings and hiding errors is just as simple as adding a single line of code. To show warnings and notices, the parameter for the error reporting function will be “E_WARNING | E_NOTICE”. The error_reporting function can accept E_ERROR, E_WARNING, E_PARSE, and E_NOTICE parameters as bitwise operators. To report all errors except for notices, then the parameter is “E_ALL & ~E_NOTICE” where E_ALL stands for all the possible parameters of the error_reporting function.

In-depth with the error_reporting() function

The error reporting function is a built-in PHP function that allows developers to control which and how many errors will be shown in the application. Remember, the PHP ini configuration has an error_reporting directive that will be set by this function during runtime.

error_reporting(0);

To remove all errors, warnings, parse messages, and notices, the parameter that should be passed to the error_reporting function is zero. It would be not practical to have this line of code in each of the PHP files. It would be better to turn off report messages in the PHP ini file or in the .htaccess.

error_reporting(E_NOTICE);

PHP allows variables to be used even when not declared. This is not a standard practice because undeclared variables will cause issues for the application once it is used in loops and conditions. Sometimes, this also happens because the declared variable has a different spelling than the variable being used for conditions or loops. When E_NOTICE is passed in the error_reporting function, then these undeclared variables will be displayed in the web application.

error_reporting(E_ALL & ~E_NOTICE);

The error reporting function allows you to filter which errors can be shown. The “~” character means “not” or “no” so the parameter ~E_NOTICE means not to show notices. Notice the “&” and “|” characters in between the possible parameters. The “&” character is for “true for all”, while the “|” character represents either one as long as it is true. These two characters have the same meaning in PHP conditions OR and AND.

error_reporting(E_ALL);
error_reporting(-1);
ini_set('error_reporting', E_ALL);

These three lines of codes do exactly the same thing, it will show all PHP errors. The error_reporting(E_ALL) is the most widely used among developers to show error messages because it is more readable and understandable.




Share on Pinterest
Share on LinkedIn
Share on WhatsApp
Share on Telegram