Turning on/off warnings and errors in PHP

Write a script to determine whether the page is on a local, testing, or live server, and set $state to “local”, “testing”, or “live”. Then:


if( $state == "local" || $state == "testing" )
{
 ini_set( "display_errors", "1" );
 error_reporting( E_ALL & ~E_NOTICE );
}
else
{
 error_reporting( 0 );
}

Or if you are sure your script is working perfectly, you can get rid of Warning and notices by putting this line at the beginning of your php script:

error_reporting(E_ERROR);

Before that, during the development, I would properly debug my script so that all notice or warning disappear one by one. So we should first set it as verbose as possible with:

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

To log errors instead of displaying them is to record errors into a file so only the php developer sees the error messages, not the users. A possible implementation is via the .htaccess file, useful if you don’t have access to the php.ini file:


# supress 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>