Displaying PHP Errors in Development

Here's a quick tip for PHP error reporting and display in development.

When a project is in the early stages of development you want to see all the error information you can. You probably want E_STRICT on especially when you're starting from scratch, to help avoid relying on deprecated behaviour. The E_STRICT flag is only available as of PHP 5 and is not included in E_ALL until PHP 5.2 (there's a little disagreement on php.net between the definition of E_ALL in this table and the earlier note about error_reporting on the same page).

In an early development project you also don't want to have to keep tailing log files to see the error messages. That's a pretty sure way to miss errors. So you want to set the display_errors flag on. You also want to control this on a per-project basis, since some projects will have legacy bugs that you're not fixing right now and those can be left spouting errors to logs until someday in the future when you decide to fix them.

So here's what I've done. In my php.ini the global settings are

error_reporting  =  E_ALL
display_errors = Off
log_errors = On

and so forth. Then in my new project folder I've put an .htaccess file with flags to override these settings.

php_value error_prepend_string '<pre>'
php_value error_append_string '</pre>'
php_value error_reporting 8191
php_flag display_errors On

Now my errors show up in a preformatted block on the page for this project and other stuff in other directories is left alone. I guess if I were really clever I could use a named div instead of just a <pre> tag and use CSS to relocate that block to a status area on the page. The magic number 8191 has to be used instead of E_ALL because PHP constants aren't known at the time this file gets processed. When modifying that value you'll have to do the Boolean math yourself and put the final value there as well.

Your rating: None Average: 3.5 (2 votes)