Per Directory PHP Error Reporting
« | Tue September 16, 2008 | comments and reactions | permanent link | »
Setting PHP's error reporting level on a per-directory basis can be useful for testing/debugging purposes, as well as for supporting legacy applications on servers with a more strict default error reporting level. Adding the following lines to a directory's .htaccess file will set error reporting to PHP 4 and 5 default (of E_ALL & ~E_NOTICE):
php_flag display_errors on php_value error_reporting 6135
We initially thought that perhaps we could get away with using PHP's defined constants (such as E_ALL) directly in the .htaccess file, but unfortunately .htaccess does not appear to have access to such PHP constants, and integer values must be used to set the error reporting level.
There does seem to be some confusion out there as to how these integer values are calculated, although it appears quite simple. The values are arrived at by either adding or subtracting component values, as declared in the PHP manual.
For example, E_ALL's value is 6143, and E_NOTICE's value is 8. So, by subtracting 8 from 6143, we arrive at 6135, which as noted above is equivalent to "E_ALL & ~E_NOTICE", or something like "all errors, except notices". You can always check the integer values for a given error-reporting level:
<?php ini_set("error_reporting", E_YOUR_ERROR_LEVEL); echo ini_get("error_reporting"); ?>
Of course, you can substitute any error reporting values. For maximum error reporting and warnings (as recommended for any PHP5+ dev environment):
php_flag display_errors on php_value error_reporting 8191
*You should note that these integer values may produce unexpected results in future versions of PHP and are not guaranteed to be forward compatible. From the PHP site:
Using named constants is strongly encouraged to ensure compatibility for future versions. As error levels are added, the range of integers increases, so older integer-based error levels will not always behave as expected.
...which of course is problematic, as we cannot use named constants in .htaccess. So, the caveat here is that this per-directory .htaccess error level setting may need updated if/when you upgrade PHP versions. (PHP 6 rolls E_STRICT into E_ALL, so the recommended practice to ensure maximum error reporting "from now and well into the future" is to simply use a large "numeric value like 2147483647".)