CHAPTER 7 – Error Reporting
Several php.ini configuration settings control which errors should be displayed and how. error_reporting (Integer) This setting is the default error reporting for every script. The parameter may be any of the constants listed here, E_ALL for everything or a logical expression such as E_ALL & ~E_NOTICE (for everything except notices).
display_errors (Boolean) This setting controls whether errors are displayed as part of PHP's out- put. It is set to On by default. display_startup_errors (Boolean) This setting controls whether errors are displayed during PHP startup. It is set to Off by default and is meant for debugging C extensions. error_prepend_string (String) This string is displayed immediately before the error message when dis- played in the browser. error_append_string (String) This string is displayed immediately after the error message when dis- played in the browser. track_errors (Boolean) When this setting is enabled, the variable $php_errormsg is defined in the scope PHP is in when an error occurs. The variable contains the error mes- sage. html_errors (Boolean) This setting controls whether HTML formatting is applied to the error message. The default behavior is to display HTML errors, except in the CLI version of PHP (see Chapter 16, "PHP Shell Scripting"). xmlrpc_errors (Boolean) This setting controls whether errors should be displayed as XML-RPC faults. xmlrpc_error_number (Integer) This XML-RPC fault code is used when xmlrpc_errors is enabled. log_errors (Boolean) This setting controls whether errors should be logged. The log destina- tion is determined by the error_log setting. By default, errors are logged to the web server's error log. log_errors_max_len (Integer) This is the maximum length of messages logged when log_errors is enabled. Messages exceeding this length are still logged, but are truncated. error_log (String) This setting determines where to place logged errors. By default, they are passed on to the web server's error-logging mechanism, but you may also specify a file name, or syslog to use the system logger. Syslog is supported for UNIX-style systems only. ignore_repeated_errors (Boolean) When enabled, this setting makes PHP not display the exact same mes- sage two or more times in a row. ignore_repeated_source (Boolean) When enabled, PHP will not display an error originating from the same line in the same file as the last displayed error. It has no effect if ignore_repeated_errors is not enabled.
Here is a good set of php.ini error-handling settings for development servers: error_reporting = E_ALL display_errors = on html_errors = on log_errors = off Notices are enabled, which encourages you to write notice-safe code. You will quickly spot problems as you test with your browser. All errors are shown in the browser, so you spot them while developing. For production systems, you would want different settings: error_reporting = E_ALL & ~E_NOTICE display_errors = off log_errors = on html_errors = off error_log = "/var/log/httpd/my-php-error.log" ignore_repeated_errors = on ignore_repeated_source = on Here, no error messages are displayed to the user; they are all logged to /var/log/httpd/my-php-error.log. HTML formatting is disabled, and repeat- ing errors are logged only once. Check the error log periodically to look for problems you did not catch during testing. The important thing to keep in mind is that error messages printed by PHP are meant for developers, not for the users of the site. Never expose PHP error messages directly to the user, catch the error if possible, and present the user with a better explanation of what went wrong.