How to Disable Error and Warning Messages in PHP
When encountering unexpected errors and notices in your PHP script, such as fsockopen() warnings and A non well formed numeric value notices, you may want to suppress them. This can be particularly useful when using cron tasks to prevent logging of these messages.
To achieve this, simply add the following line to the beginning of your PHP script:
error_reporting(E_ERROR);
Alternatively, if you wish to receive detailed debugging information, set error reporting to verbose mode:
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
Logging Errors Instead of Displaying Them
A more sophisticated approach to handling errors is to log them into a file, allowing only developers to view error messages while concealing them from users. This can be implemented through the .htaccess file, especially if the php.ini file is inaccessible:
# Suppress PHP errors 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 # Enable PHP error logging php_flag log_errors on php_value error_log /home/path/public_html/domain/PHP_errors.log # Prevent access to PHP error log <Files PHP_errors.log> Order allow,deny Deny from all Satisfy All </Files>
Remember to replace "/home/path/public_html/domain/" with the appropriate directory path.
The above is the detailed content of How to Handle and Suppress PHP Error and Warning Messages?. For more information, please follow other related articles on the PHP Chinese website!