elepha.info

Custom PHP Error displaying

08. 10. 06.

I was told one cool feature (from Jakub Roztočil) how to customize displaying of errors in PHP. So let me share it with you.

I assume you are using Apache server, using decent browser (not including IE) and have few moments to set this up.

All magic is in changing few lines in your php.ini file. Look for error_prepend_string and error_append_string. In most cases they will be commented, so uncomment them and set their values as below:

error_prepend_string = "<php:error>"
error_append_string = "</php:error>"

Now comes funny part. Open your favourite editor and write simple user.css stylesheet file for your decent browser. Mine looks like this:

 1: php\:error{
 2:         border: 1px solid #333;
 3:         padding: 3px 5px;
 4:         background:  #000;
 5:         color: #fff;
 6:         font-size: 0.84em;
 7:         font-family: "Lucida Grande", sans-serif;
 8:         position: relative !important;
 9:         z-index: 10000 !important;
10: }
11: 
12: php\:error br{
13:         display: none;
14: }
15: 
16: php\:error b{
17:         color: red;
18: }
19: 
20: php\:error br+b:before{
21:         content: "PHP ";
22: }

Don't forget to use the stylesheet set default in your browser. Otherwise this whole thing will not work. You can also edit stylesheet as much as generated HTML and your and yours browser knowledge of CSS will allow you. I haven't tested it yet but use of JavaScript is not ommited.

Now when you write an error file and lets say throw exception, PHP error whitch will be displayed should look like this.

You will have never to look after any hidden php error (mostly warnings and notices) in your application while developing it.

This tutorial was tested on Windows 2000 with Apache 2.0 and PHP 5.1 on my workstation in office and MacOS X 10.4.8 with Apache 1.3 and PHP 5.1. Both computers used Opera 9 as decent browser.

Author does not take any responsibility of damaging your data while setting or using setup written in this tutorial.

Submit a comment

Author info

Comment

You can respond to comment by writing [commentno] where commentno is number of comment displayed near author's name. Formatting is one of the easiest parts **text** will result in text, *text* will result into text and "Google":[http://www.google.com] will be formated into Google. No (X)HTML is allowed.


Browse by tags