PHP Error reporting

From Notes_Wiki
Revision as of 13:27, 7 April 2022 by Saurabh (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Home > PHP > PHP Error reporting

PHP error reporting configuration can be done in many places:

  • php.ini
  • httpd.conf
  • .htaccess (if AllowOverride is enabled for VirtualHost)
  • script file

The error reporting can be set with the help of predefined constants E_ALL, E_NOTICE, E_DEPRECATED, etc. in php.ini file and in scripts. Hence it is better to put error_reporting settings in local scripts or some global include file (for site wide settings) rather then in httpd.conf or .htaccess. The problem httpd.conf or .htaccess configuration is that we cant use constants E_ALL, etc. instead we have to use their values. These values can and usually do change from one version of PHP to another as new constants get declared. Configuring error_reporting with predefined constants in php.ini or script ensures when we migrate code to newer version of PHP the settings still carry same meaning.


php.ini configuration

In php.ini we can configure

display_errors = On 
error_reporting = E_ALL & ~E_NOTICE

to get all errors except notices. For sites that were built with error reporting off, notices can be very painful to remove. But for new codes it is recommended that one keeps 'error_reporting = E_ALL' on all development machines.

  • Please verify that you are editing correct php.ini file by confirming full path of file from phpinfo page. Use:
<?php phpinfo(); ?>

to create phpinfo page.


script file configuration

Since php.ini settings affect entire server. To change settings for only one site or single page it is preferable to change error reporting settings using code. We can use

error_reporting(E_ALL);

or

ini_set('error_reporting', E_ALL);

to enable error reporting for all types of error from code. If this is put as line in global configuration file which gets included in all pages of site then it can be used to change site wide error reporting.

  • Care must be taken to remove error reporting while transferring code from development machine to production machine to hide internal details of program/databases/filesystem etc. if code breaks.


httpd.conf

In httpd.conf we can set error reporting for particular VirtualHost using:

php_flag  display_errors on
php_value error_reporting 30719    #30719 is equivalent of E_ALL in PHP 5, As mentioned
                                   #before value changes from version to version. Hence this
                                   #method should be avoided if above two methods can get things done.


.htaccess

If 'AllowOverride Options' or 'AllowOverride All' is enabled for some 'Location', 'Directory' or 'VirtualHost' in 'httpd.conf' file then we can use:

php_value error_reporting 2047  #The value has to be calculated
                                #based on PHP version in use
php_flag display_errors true
php_flag display_startup_errors true
php_flag log_errors true
php_value log_errors_max_len 100M
php_flag ignore_repeated_errors false
php_flag ignore_repeated_source false
php_flag report_memleaks true
php_flag track_errors true
php_flag html_errors false
php_value error_log /path/to/error/log

This options to set various parameters regarding php error reporting. Note that these settings can be changed in locations 'PHP_INI_ALL' which effectively means 'Entry can be set anywhere in code, php.ini, httpd.conf or .htaccess'.

Hence, although lot of options are changed in .htaccess examples and not in others, it does not means that .htaccess provides more options. All these options can be changed using all four methods discussed in this page.


Home > PHP > PHP Error reporting