Home > Database > Mysql Tutorial > How to Handle and Suppress PHP Error and Warning Messages?

How to Handle and Suppress PHP Error and Warning Messages?

DDD
Release: 2024-11-10 09:03:02
Original
874 people have browsed it

How to Handle and Suppress PHP Error and Warning Messages?

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);
Copy after login

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);
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template