PHP tip: Use error_log() to email debugging info

Have you ever come across a situation where you can't echo debugging information on-screen because the problem code is live, or the display_errors configuration directive is off? PHP's error_log() is a handy utility function that can be used to email instead of echo'ing on-screen. I like to combine it with print_r() for formatting. For example, let's say you need to see all of the data in the $_SESSION superglobal:

error_log(print_r($_SESSION, TRUE), 1, 'benrothe@gmail.com');

The second parameter to error_log(), 1 in this case, sends the contents of the first parameter to the email address specified in the third parameter. I like to use TRUE as the second parameter to print_r() to return it's output instead of printing it (which is does by default). The result is anicely formatted email in your inbox like so:

From: Apache <apache@some.domain.com>
Sent: Wednesday, December 24, 2008 10:20 AM
To: benrothe@gmail.com
Subject: PHP error_log message

Array
   (
      [member_count] => 3567120
      [member_id] => 123456
      [last_login] => 2008-12-24 10:05:25
      [first_name] => Ben
      [last_name] => Rothe
      [opt_out] => 0
   )

Add your thoughts...

Name:

Website/URL:
(optional)

Comments:

Image Verification: