# PHP error reporting

## Error levels

 **name** **value** **description** **example 1** **example 2** E\_ERROR 1 Fatal run-time errors notdefined(); E\_WARNING 2 Run-time warnings 1 / 0; E\_PARSE 4 Compile-time parse errors +-; E\_eval(‘+-;’); E\_NOTICE 8 Run-time notices print $i\_am\_not\_defined; E\_CORE\_\* varies generated by <span class="caps" id="bkmrk-php">PHP</span> core E\_USER\_\* varies generated by [trigger\_error](http://us2.php.net/trigger_error) E\_ALL 2047 Everything## Custom Error Handler

It can only catch E\_WARNING, E\_PARSE, E\_USER\_ERROR, E\_USER\_WARNING and E\_USER\_NOTICE. If something else happens, PHP’s default error handler takes place.

```
functional my_error_handler($errno, $error, $file, $line) {  ... handle the error ...}
```

set\_error\_handler(‘my\_error\_handler’);

## Apache

In Apache, you can customize how it responses to a particular <span class="caps">HTTP</span> status code. It can show a message or load a specific page. e.g. (from Apache’s doc)

```
ErrorDocument 500 /cgi-bin/crash-recoverErrorDocument 500 "Sorry, our script crashed. Oh dearErrorDocument 500 <a href="http://xxx/">http://xxx/</a>ErrorDocument 404 /Lame_excuses/not_found.htmlErrorDocument 401 /Subscription/how_to_subscribe.html
```

However, when executed as an Apache module <span class="caps">PHP</span> returns a <span class="caps">HTTP</span> status code of 200 (OK) even if there is an compile or run-time error. (Not sure if it’s a bug or a feature, since this behavior is not mentioned anywhere else.) Therefore, one cannot use Apache’s custom error capability for <span class="caps">PHP</span> errors.

Instead, either use a custom error handler (see above) or have <span class="caps">PHP</span> wrap the message inside some specific <span class="caps">HTML</span> code (see below).

## Pretty-printing Error Messages

Config keys error\_prepend\_string and error\_append\_string are used to determine the <span class="caps">HTML</span> code that prepends or appends an error message.

e.g. To display each error message inside a box, put these in php.ini:

```
; String to output before an error message.error_prepend_string = "<fieldset>An error has occured. Please notify the <a href=\"mailto:<a href="mailto:admin@yourname.com">admin@yourname.com</a>\">administrator</a> with the following error message: <br>"; String to output after an error message.error_append_string = "</fieldset>"
```

To make the change effective to a directory instead of system-wide, put these in .htaccess:

```
php_value error_prepend_string "(... starting tags ...)"php_value error_append_string "(... ending tags ...)"
```

Note: Because any compile-time error stops the compilation, there will be at most one box.