Suppressing Errors and Notices in PHP and MySQL
While developing PHP scripts, you may encounter various warnings and notices that can be overwhelming. These messages can distract from the core functionality of the script and make it difficult to debug. To mitigate this, you can opt to turn off these warnings and notices.
One such error is:
Warning: fsockopen()
Additionally, you may see the following notice:
Notice: A non well formed numeric value encountered in
If you plan to execute this PHP script using cron, you may prefer to disable these messages to prevent them from being logged.
Disabling Warnings and Notices
To turn off warnings and notices, you can insert the following line at the beginning of your PHP script:
error_reporting(E_ERROR);
This setting suppresses all warning and notice messages, leaving you with only error messages.
Logging Errors Instead of Displaying Them
As an alternative to disabling errors and notices, it is recommended to log these messages into a file. This way, only the developer can access these error messages, preventing them from being displayed to users.
To implement this solution using a .htaccess file:
php_flag display_startup_errors off php_flag display_errors off php_flag html_errors off php_value docref_root 0 php_value docref_ext 0
php_flag log_errors on php_value error_log /home/path/public_html/domain/PHP_errors.log
<Files PHP_errors.log> Order allow,deny Deny from all Satisfy All </Files>
By implementing this solution, you can ensure that errors are recorded and accessible only to authorized individuals.
The above is the detailed content of How to Suppress and Log PHP and MySQL Errors and Notices?. For more information, please follow other related articles on the PHP Chinese website!