A PHP application may produce many different levels of errors and warnings when executed. Viewing these errors is critical for developers to troubleshoot an application. However, difficulties are often encountered when trying to display errors from PHP applications, which often fail silently.
Quickest Way to Show All PHP Errors
Adding the following lines to your PHP code is the quickest way to show all PHP errors and warnings:
The above functions are directives work as follows:
ini_set function tries to override the configuration found in the PHP .ini file.
display_errors is a directive that determines whether the errors will be shown to the user or remain hidden. This should usually be disabled after development.
display_startup_errors is also a directive, which is used to find the errors encountered during the PHP startup sequence. This is a separate directive because the
display_errors directive does not handle such errors.
display_startup_errors directives do not show parse errors such as missing semicolons or curly braces. The PHP ini configuration must be modified to achieve this.
error_reporting is a native PHP function that is used to show errors. This function can be used to report all types of errors in the PHP script. For that, the named constant
E_ALL is used as the argument in the function.
Configure PHP.ini to Display All Errors and Warnings
If adding the above functions and directives does not show all errors, the PHP ini configuration has additional directives that can be modified:
display_errors = on
display_errors directive can be set to "
on" in the PHP.ini file. This will display all errors including syntax and parse errors that are not displayed by only calling the
ini_set function in the PHP code.
Note that the
display_errors directive must be set to "
off" if the application is in production.
The PHP.ini file can be found in the output of the
Loaded Configuration File shows the location of the PHP.ini file.
The PHP error_reporting() function
The error reporting function is a built-in function in PHP that allows developers to specify which and how many errors are shown in the application. This function sets the
error_reporting directive in the PHP ini configuration during runtime.
The value 0 should be passed to the
error_reporting function to remove all errors, warnings, parse messages and notices.
Variables are allowed to be used in PHP even if they are not declared. This is not best practice as undeclared variables cause issues if used in loops and conditions. Undeclared variables are displayed in the web application when
E_NOTICE is passed in the
error_reporting(E_ALL & ~E_NOTICE);
error_reporting function allows developers to filter which PHP errors can be shown. The “~” character stands for "not" or "no", so the parameter
~E_NOTICE means not to show notices. The "&" character means "true for all".
These 3 lines of code do the same thing - they show all PHP errors. The
error_reporting(E_ALL) is the most widely used since it is more readable.
Show PHP Errors Through .htaccess Configuration
Directory files are usually accessible to developers. The .htaccess file located in the root or public directory of the project can also be used to enable or disable the directive for showing PHP errors.
php_flag display_startup_errors on
php_flag display_errors on
The .htaccess file has directives for
display_errors, similar to what will be added to the PHP code to show errors. Development and production can have different .htaccess files by showing or disabling error messages this way, with production suppressing the displaying of errors.
display_errors directive in .htaccess or the PHP.ini file may need to be configured depending on which files are accessible and how deployments and server configurations are done. Many hosting providers will not allow changes to the PHP.ini file to enable
Track, Analyze and Manage PHP Errors With Rollbar
Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing and showing PHP errors easier than ever. Try it today