Last Updated: May 27, 2020
·
3.597K
· passcod

Unobstrusive debug output in PHP

Sometimes you want to echo out a value but you also know there's headers getting sent further down. Instead of screwing up the output:

header('X-Debug: '.$value);

...and inspect the result in Firebug's Net tab (or Chrome equivalent).

8 Responses
Add your response

How obvious, yet I never came up with that myself, neither have I read it anywhere else. I hate this "how come I didn't think of that before" feeling. Up-vote!

over 1 year ago ·

Or use firePHP

over 1 year ago ·

Nice tip =)

over 1 year ago ·

Packfire has a debugger that uses Firebug output.

over 1 year ago ·

You could also log to syslog, which is standard of logging for quite a while... Windows has a similar solution.

Headers shouldn't be messed with. Firebug is known for causing problems due to the large header sizes. Besides that, is using "X-" for headers deprecated behaviour, see: http://www.ietf.org/rfc/rfc6648.txt

over 1 year ago ·

I like using Chrome PHP http://www.chromephp.com my only issue with the header() is that you must call if before you generate any output.

I love using xdbug with PHPstorm as well

over 1 year ago ·

Also:

if (isset($_GET['debug']) && $_GET['debug'] == 'true')
{
    header('X-Debug: '.$value);
}

To control whether or not the header gets sent. (Performance Optimization?)

over 1 year ago ·

@matthewbj The performance optimisation is to not use it at all when not doing debugging. This trick is the equivalent of peppering console.log calls in JS: it's highly inefficient but very useful when doing quick checks. If you want a long-term debug log solution, look into proper logging (to file or tty or socket) so you don't mix things in the output.

over 1 year ago ·