How to Display PHP Errors
Often errors occur while executing a PHP application. Read this snippet to learn how to overcome difficulties and display such errors with the help of PHP.
Different errors can occur during the execution of a PHP application. Developers often meet obstacles while trying to display such errors. If you also have issues with your PHP application and want to display the errors, you are in the right place.
The Quick Display of a PHP Error with ini_set
If you want to quickly show all the PHP errors and warnings, then you should add the following lines to your code:
php error add lines
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>The <kbd class="highlighted">ini_set</kbd> function may allow overriding the configuration detected in the PHP ini file. Different directives can be available. Two of them are <kbd class="highlighted">display_errors</kbd> and <kbd class="highlighted">display_startup_errors</kbd> that are demonstrated in the example above. The first directive, as a rule, determines whether the errors are going to be shown to the user or hidden. After development, it should be turned off.
The <kbd class="highlighted">display_startup_errors</kbd> directive is considered a separate one, as the <kbd class="highlighted">display_errors</kbd> doesn’t deal with the errors occurring during the startup sequence.
It is essential to note that the directives above can’t parse errors like missing curly braces or missing semicolons. In such cases, it is necessary to modify the PHP ini configuration.
Using Configuration of php.ini
The <kbd class="highlighted">php.ini</kbd> configuration has additional directives aimed at handling and showing the errors in the browser during testing.
To use it, you need to act like this:
display errors php
display_errors = onSo, you need to turn on the <kbd class="highlighted">display_errors</kbd> directive inside the <kbd class="highlighted">php.ini</kbd> file. It can display all the errors, particularly, parse and syntax errors that can’t be shown by calling only the <kbd class="highlighted">ini_set</kbd> function. That file can be located in the displayed output of the <kbd class="highlighted">phpinfo()</kbd> function. When the web application is in production, this directive should be set to off.
Displaying Errors with the .htaccess Configuration
With the <kbd class="highlighted">.htaccess</kbd> file, a directive for displaying errors can be enabled or disabled like this:
php flag display errors
php_flag display_startup_errors on
php_flag display_errors onThe <kbd class="highlighted">.htaccess</kbd> file also encompasses directives such as <kbd class="highlighted">display_startup_errors</kbd> and <kbd class="highlighted">display_errors</kbd>.
Inside this file, a custom error log may be enabled, provided the log file or directory is writable by the web server. The log file can be a path relative to where the <kbd class="highlighted">.htaccess</kbd> file is placed, or it may be an absolute path like /var/www/html/website/public/logs. To configure this, add the following directives:
php_value log_errors 1
php_value error_log /var/www/html/website/public/logs/php_errors.logNote: These directives only work when PHP runs as an Apache module (mod_php). They are ineffective with PHP-FPM or CGI setups.
Displaying Fatal Errors
If you only want to display fatal errors and not warnings or notices, you can modify the error reporting level to exclude non-fatal errors. Here's an example:
Displaying Fatal Errors in PHP
<?php
error_reporting(E_ERROR);
ini_set('display_errors', 1);In this example, the error_reporting function is set to only report fatal errors, using the E_ERROR constant. This constant represents the most severe type of error in PHP and includes fatal errors, which halt the script execution, but not warnings or notices.
The ini_set function is used to turn on the display of errors, just as in the previous example. This will display any fatal errors that occur in your PHP code on the web page.
Note that this approach will only display fatal errors and suppress all other errors, including warnings and notices. This can be useful in some cases, but it may also mask other issues that could be important for debugging and troubleshooting your code. It's generally recommended to fix all errors and warnings, and to use a comprehensive error reporting system that logs errors and sends notifications to developers or administrators.