TIP: Debugging Drupal (and PHP applications)


"The most effective debugging techniques seem to be those which are designed and built into the program itself."

- Donald E. Knuth (1993)

For all of the wonders of full scale Debugger debugging (as possible with XDebug + NetBeans IDE, see the external links), there is - despite all protestations from certain Drupal contributed module coding standard devotees - still much to be said for leaving debug statements right there in the code using the Devel module's dpm() or other debug functions, possibly under further masking control of a simple define('MYMODULE_DEBUG',TRUE/FALSE) switch, or under the control of a module-specific DEBUG mode switch settable via a Drupal form.

Although this is definitely frowned upon in Drupal contributed modules, when developing custom modules for a client who does not care and will never see, such debug statement in code can leave (as Knuth and many others have observed) a very useful commentary record about what was done and why. I am not talking here about massively littering the code with debug statements that are basically mere dpm() echo statements, I am talking about informative debug statements that can, for example, be switched back on if needed on a STAGING Drupal instance, where full Debugger debugging is usually not practical anyway.

In short: don't be scared to use good ol' fashioned line-by-line debug output if needed, and if it gets the job done quickly for you. Dr Darren says it's ok, lots of eminent computer scientists say it's ok, and damn those who complain about it.

Besides, if Angie Byron of Lullabot is allowed to HACK drupal_set_message() to include debug_backtrace(), I am allowed to use dpm() and lowly friends.

Now enjoy the rest of this zone to find out some fancier ways of debugging Drupal it (that may or may not bring additional benefit).

Dr Darren, The Drupal Heretic

Visit also