What is the best method of hiding php errors from being displayed on the browser?
Would it be to use the following:
ini_set("display_errors", 1);
Any best practice tips would be appreciated as well!
I am logging the errors, I just want to make sure that setting the display_errors value to off (or 0) will not prevent errors from being logged.
The best way is of course to log your errors instead of displaying or ignoring them.
This example will log the errors to syslog instead of displaying them in the browser.
ini_set("display_errors", 0);
ini_set("log_errors", 1);
//Define where do you want the log to go, syslog or a file of your liking with
ini_set("error_log", "syslog"); // or ini_set("error_log", "/path/to/syslog/file");
Assuming you are in control of the php.ini file, you can make these changes globally inside that file instead of having the ini_set code laying around in all your php files (which you might forget to put in one of your files one day, which could be bad in production).
set an environment variable on your development machine. then you can check your environment in the beginning of your code to make sure you are on the development environment.
then you can set the error reporting level of PHP, by using the error_reporting() function.
if ( isset($_ENV['MY_APP_MODE']) && ($_ENV['MY_APP_MODE'] == 'devel') ) {
error_reporting(E_ALL);
} else {
error_reporting(0);
}
You can also use apache .htaccess to log errors without touching your code:
<IfModule mod_php5.c>
php_flag display_errors Off
php_flag log_errors On
php_value error_log logs/errors
</IfModule>
The PHP distribution files unfortunately sends a mixed message about handling error messages. The default php.ini file has some settings suitable for production servers and others better for development servers. You should turn all errors on in dev (error_reporting = E_ALL
and display_errors = On
), and off in prod (display_errors = off
but point log_errors
somewhere). However, do your best to make sure there are no errors (or they're handled).
The best way? Fix the errors!
If you need a quick temporary solution then it's OK to turn display_errors off, but make sure you're logging them somewhere, because that setting will make serious/parse errors silent otherwise.