How to set error level and output in PHP
Published in Blog, Uncategorised by Adam Williams / 0 CommentsYou can set the error reporting level in PHP and also define whether it is displayed to the user or saved to a log file.
The level can be defined either in the php.ini, .htaccess or directly inside a PHP script.
The logging level options are:
- E_ALL – will log every error, including notice messages
- E_WARNING – will log errors which are not fatal
- E_ERROR – Will log fatal errors
- E_NOTICE – Will log notices only, such as using a constant which hasn’t been defined.
The logging levels can be combined to suit using ampersands (&). The tilder character (~) can also be used before an option to exclude it. An example would be
error_reporting E_ALL & ~E_NOTICE
The above example will show all errors except notices. This is probably the combination you are most likely to use and is the default setup in most configurations.
Here’s some examples of how to use error_reporting within the various files:
php.ini
error_reporting = E_ALL & ~E_NOTICE
.htaccess
php_value error_reporting E_ALL & ~E_NOTICE
Inside a PHP script
ini_set('error_reporting', 'E_ALL & ~E_NOTICE');E_ALL & ~E_NOTICE
If you would like to display the errors to the users you should also add the following line in the same way as above:
display_errors On / php_value display_errors 1 / ini_set('display_errors', '1');
If you would prefer to just log the errors to a file and not display anything on screen just set the above to 0 instead of 1, then add the following directive in the same way as above:
log_errors 1 / php_value log_errors 1 / ini_set('log_errors', '1');
The errors will be stored in your servers Apache error log.
Tags: .htaccess, error_reporting, ini_set, php, php.ini