If you debug your PHP code, you often get too many messages if you just use echo and print_r() or var_dump(). In the last few weeks I have implemented some useful techniques to prevent bunches of debugging messages on my screen:
- If you debug a template and you want to see the true result without any messages – but you don’t want to remove them or comment them out, you define a constant that tells your script whether it’s going to debug or not and you define your own function for debugging. This function checks if your constant is
trueand then prints your message. - You can make it more comfortable and make your debug function accept any number of arguments of of different types. They will be conactenated and printed out in a meaningful way. So a
falsewon’t be a blank string, an array is printed viaprint_r()orvar_dump() - Arrange your messages neatly by printing them in the end of your script – or use the killer feature: Send them to your Firebug console. Now your page is clean and you use full featuritis of Firebug, that will show your objects and recursive structures in a much more developer friendly way ;) When you test your site in other browsers, you can change output mode to HTML – your messages are collected in a box, and that’s it.
E.g.:
define('debug', 1);
define('debugMode', 'js'); // 'html' or 'js'
...
function myMethod($arg1, $arg2) {
$_ = func_get_args();
debug(__method__, $_);
... code ...
debug("\twhat I wanna say", array('that' => 'sucks'));
}
// print all messages at once
debugChain();
I don’t want you to do all the work again: Download the package rename file extension to “.php” and use it: debugPackage0.1._php