Monday, February 21, 2011

PHP: Stop Notice and Variable Warnings in Error Log Files

SkyHi @ Monday, February 21, 2011
Q. I'm using error logging in place of error displaying on production web sites. However, I do not want to see error messages about the use of uninitialized variables. I'd like to see all critical errors, except for notices and coding standards warnings. How do I disable error messages?

A. PHP provides various levels of error reporting using a bit-field as follows:

Error BitPurpose
E_ALL All errors and warnings (doesn't include E_STRICT)
E_ERROR Fatal run-time errors
E_WARNING Run-time warnings (non-fatal errors)
E_PARSE Compile-time parse errors
E_NOTICERun-time notices (these are warnings which often result from a bug in your code, but it's possible that it was intentional (e.g., using an uninitialized variable and relying on the fact it's automatically initialized to an empty string)
E_STRICTRun-time notices, enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code.
E_CORE_ERRORFatal errors that occur during PHP's initial startup
E_CORE_WARNING Warnings (non-fatal errors) that occur during PHP's initial startup
E_COMPILE_ERROR Fatal compile-time errors
E_COMPILE_WARNING Compile-time warnings (non-fatal errors)
E_USER_ERROR User-generated error message
E_USER_WARNINGUser-generated warning message
E_USER_NOTICEUser-generated notice message

Show only errors

Open /etc/php.ini file
# vi /etc/php.ini

Set error_reporting as follows:
Alternately, you can show all errors, except for notices and coding standards warnings
error_reporting = E_ALL & ~E_NOTICE
Save and close the file. Restart apache web server:
# /etc/init.d/httpd restart

I am running my web site on a shared host.
In my case the /etc/php.ini is not accessible, but they allow me to use local php.ini in each folder.
Other web hosting companies allow to modify .htaccess, so the alternative solution for this:

error_reporting = E_ALL & ~E_NOTICE

when using .htaccess is:

php_value error_reporting 2039