print_r is not pretty
I feel that PHP's print_r() function and Drupal's Devel module's dprint_r() function's output is not pretty or friendly because the generated code needs to be interpretted and translated into a usable PHP string that can then be inserted into a PHP template or script. So I decided to write a new debugging function, called dprint_rx(). My dprint_rx() function outputs a list of usable PHP strings, enhanced with some color coding and JavaScript, that can be easily cut-n-pasted directly into a PHP template or script.
Here is an example of my dprint_rx() function's output compared to the Devel module's dprint_r() function's output.
Right now, this is just an experimental idea. I am very open to contributing this code as a patch to the Devel module. First I would like to get some feedback and help resolving some issues.
My current issues and questions with my dprint_rx() function are:
- Should this function be called dprint_rx()? Should the function be called something different, since the output is actually completely different then the dprint_r() function's output? I find using the names print_rx() and the alias dprx() easy to remember.
- The dprint_rx() function works best when the variable names are passed into the function using an associated array formatted as dprint_rx(array('$variable_name' => $variable_data)). I have found no easy or reliable way to backtrace the actual name of the variable passed into the dprint_rx() function. Any help or guidance would be appreciated.
- The dprint_r() function's recursion/depth limit is set to 9 levels of nested data which I based on the outer limit of the 'seven, plus or minus two' rule. Is this a reasonable cut-off?
- The dprint_rx() function's output's colors and JavaScript is inspired by Coldfusion's <CFDUMP> tag. I think the color and javascript is fine but I am open to tweaking the output. For example, I could add open and close arrows infront of the clickable array and object titles.
Here is the code which I have placed in a custom module that I am calling the 'devel_helper'. I tend to call my custom modules helpers since they are usually trying to 'help' improve an existing module or concept. My demo Drupal website has the very basic API documentation for the devel_helper module available for online review.
Thanks.