PHP programmers often encounter various errors during the development process. In order to facilitate error checking, PHP will output error prompts by default. But in a production environment, these error messages may not only expose your code structure, but also affect the performance and security of the website. Therefore, turning off PHP error messages is a necessary and simple operation.
Next we will discuss in detail how to turn off PHP error prompts.
In the PHP installation directory, there is a configuration file named "php.ini" . This file records various configuration information of PHP, including error message information. We can turn off PHP error prompts by modifying the relevant configuration in this file.
First you need to find the php.ini file. Generally, in the PHP installation directory, there will be a file named "php.ini". If you can't find it, you can enter php --ini on the command line to view PHP information, including the path to the php.ini file.
Use a text editor to open the php.ini file and find the following two lines of code:
display_errors = On error_reporting = E_ALL
Change "On" is changed to "Off", which means turning off the error prompt. At the same time, change "E_ALL" to "0" to turn off all error reporting. The modified code is as follows:
display_errors = Off error_reporting = 0
Save the changes and exit the editor.
After modifying the php.ini file, you need to restart PHP for the settings to take effect. You can enter sudo service php7.2-fpm restart
on the command line to restart Apache or Nginx to make the new php.ini configuration take effect.
In some cases, we may not have permission to modify the php.ini file. At this time, we can turn off PHP error prompts by adding code to the PHP file.
Add the following code at the beginning of the PHP file that needs to turn off error prompts:
ini_set('display_errors', 'Off'); error_reporting(0);
In this way, we turn off error prompts in the development environment, protecting the security of the code and Improved website performance.
However, it is recommended to use log files to record error information in a production environment so that subsequent errors can be analyzed and the program can be improved.
The above is the detailed content of How to close php error prompt? Sharing methods between the two. For more information, please follow other related articles on the PHP Chinese website!