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).
Written by Félix Saparelli
Related protips
8 Responses
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!
Or use firePHP
Nice tip =)
Packfire has a debugger that uses Firebug output.
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
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
Also:
if (isset($_GET['debug']) && $_GET['debug'] == 'true')
{
header('X-Debug: '.$value);
}
To control whether or not the header gets sent. (Performance Optimization?)
@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.