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!
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/44392/df8e5930113ab9844be90e1271e153fb.jpeg)
Or use firePHP
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/43863/1ed7e9c97959e77569e0f8a22413cc93.jpeg)
Nice tip =)
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/46258/4749d60af0d45cc6ed8eaf014edb1f65.jpeg)
Packfire has a debugger that uses Firebug output.
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/11491/me-2018dxo.jpg)
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
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/34511/2b8ec77cceb2021240aa15f6a6b6fb8d.jpg)
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
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/47688/254fa2edb94eb13aff2d7e5b4c2a185c.jpeg)
Also:
if (isset($_GET['debug']) && $_GET['debug'] == 'true')
{
header('X-Debug: '.$value);
}
To control whether or not the header gets sent. (Performance Optimization?)
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/21332/11904d2d1af36d532f9d78d9d59f6819.png)
@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.