How to show all errors in PHP 8
July 6, 2022
How to display all errors in PHP 8
By default, PHP 8 disables showing errors, so if there is a problem while executing a PHP script, nothing will be displayed on the screen. If an error in the program occurred before the output of the HTML code, then you will see a white screen of the web browser.
Where is the error output configured in PHP
Error output is configured in:
- script code
- .htaccess file
- in the PHP configuration file (for example, in php.ini)
The settings in the script code only affect the behavior of the program in which the settings are made.
The settings in the .htaccess file affect all scripts in that directory and subdirectories.
The settings in the php.ini configuration file affect all PHP scripts that are run unless their error output settings are overridden.
Remember that error reporting is very useful while writing and debugging code, but on production servers, error reporting should be turned off to prevent sensitive data from being leaked and making it harder for an attacker to hack the site.
Configuring error output in PHP script
To display all errors, add the following lines to the beginning of the script:
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
These settings enable the output of all errors and warnings to the user's web browser.
Warnings about the use of deprecated constructs will be displayed.
Error output to the web server logs is configured separately.
Remember that if fatal errors occur, that is, when the script could not even run due to incorrect PHP syntax, then the rules specified in the php.ini or .htaccess file will be used to output errors. This is due to the fact that if the syntax is incorrect, the PHP interpreter does not understand the entire file, including the above directives. That is, if a semicolon or a curly brace is missing in the code, then errors will be displayed in accordance with the settings in the php.ini file.
Configuring PHP error output in .htaccess file
Enabling error output in the .htaccess file is done by the following directives:
php_flag display_startup_errors on php_flag display_errors on
For them to work, the web server must have .htaccess files enabled.
Error output to the web server log is performed by the following directive:
php_value error_log logs/all_errors.log
Setting the output of all errors in the php.ini file
The php.ini file is the PHP configuration file.
PHP can use more than one configuration file during its operation.
Location of php.ini file:
- In Debian and derivative distributions (Ubuntu, Linux Mint, Kali Linux and others), it depends on the PHP version, for example, for PHP 8.1 the path to the file is: /etc/php/8.1/apache2/php.ini
- On Arch Linux and derivative distributions (Manjaro, BlackArch and others): /etc/php/php.ini
In the php.ini file you will find the following directives:
display_errors = Off display_startup_errors = Off
To enable error reporting, replace them with:
display_errors = On display_startup_errors = On
The default value of error_reporting is set to:
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
This means that all errors are printed except for deprecation warnings and warnings caused by strict code checking.
To display all errors and warnings, set the following value:
error_reporting = E_ALL
Common Values:
- E_ALL (Show all errors, warnings and notices including coding standards.)
- E_ALL & ~E_NOTICE (Show all errors, except for notices)
- E_ALL & ~E_NOTICE & ~E_STRICT (Show all errors, except for notices and coding standards warnings.)
- E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors)
See the link for details: https://www.php.net/manual/errorfunc.constants.php
In order for the changes made in the php.ini file to take effect, a restart of the web server is required.
- In Debian and derivative distributions (Ubuntu, Linux Mint, Kali Linux and others), this is done with the command:
sudo systemctl restart apache2.service
- In Arch Linux and derivative distributions (Manjaro, BlackArch and others), this is done with the command:
sudo systemctl restart httpd.service
To check that the php.ini file settings are actually applied, create a file, for example, named info.php and copy into it:
<?php phpinfo();
If you created the file in the root folder of the web server, in a web browser open http://localhost/info.php.
The following screenshot shows that error output is disabled in the php.ini file:
This screenshot shows that error output is enabled in the php.ini file:
Outputting errors to the web server log
Error output to the web server log is configured in the php.ini file.
The following directive is used for this:
log_errors = On
The location of the error file is configured in the web server configuration.
The “error_reporting('all');» и ошибка «Uncaught TypeError: error_reporting()”
When trying to use the following construct:
error_reporting('all');
You will encounter the Uncaught TypeError: error_reporting() error.
Full error log:
[Wed Jul 06 07:29:19.410966 2022] [php:error] [pid 14101] [client 127.0.0.1:58402] PHP Fatal error: Uncaught TypeError: error_reporting(): Argument #1 ($error_level) must be of type ?int, string given in /srv/http/suip/index.php:3\nStack trace:\n#0 /srv/http/suip/index.php(3): error_reporting('all')\n#1 {main}\n thrown in /srv/http/suip/index.php on line 3, referer: http://localhost/suip/
Instead of 'all' you need to provide a constant expressing the level of the error message. Valid values are provided on this page: https://www.php.net/manual/errorfunc.constants.php
The following entry is correct for PHP 8 and means to show all errors, notes, and recommendations:
error_reporting(E_ALL);
Related articles:
- How to run a program from Python: how to run a system command or another Python script (complete guide) (91.9%)
- How to list columns in MySQL/MariaDB. How to check if a column exists in a table in PHP (53%)
- “Failed - Network error” when exporting from phpMyAdmin (SOLVED) (52.7%)
- How to block by Referer, User Agent, URL, query string, IP and their combinations in mod_rewrite (52.1%)
- How to protect my website from bots (52.1%)
- How to reduce image size. Bulk photo scaling and resizing on the command line (RANDOM - 22.5%)