[wp-hackers] debugging with print_r

Keith Constable kccricket at gmail.com
Fri Sep 14 22:22:07 GMT 2007

Ozh wrote:
> Hello there
> Don't know if it's because I'm dumb or something like this, but I find
> myself doing *a lot* of echo "<pre>";print_r($stuff);echo "</pre>";
> when coding something to check how things are doing.
> It's to a point that I've added the following to my wp-config.php :
> function wp_print_r($input, $pre = true) {
> 	if ($pre) echo "<pre>\n";
> 	ob_start();
> 	print_r($input);
> 	$output = ob_get_contents();
> 	ob_end_clean();
> 	$output = attribute_escape($output);
> 	echo $output;
> 	if ($pre) echo "</pre>\n";
> }
> So, heh, you know, I thought, if some day committers run out of patch
> to roll in, well, what an awesome addition it would make ;-Þ
> Cheers,
> Ozh

mixed print_r ( mixed $expression [, bool $return] )

    If you would like to capture the output of print_r(), use the return
parameter. If this parameter is set to TRUE, print_r() will return its
output, instead of printing it (which it does by default).

Of course the return argument only works in >=PHP4.3.0.  So that could
be shortened to

function wp_print_r($input, $pre = true) {
	if ($pre) echo "<pre>\n";
	echo attribute_escape(print_r($input, true));
	if ($pre) echo "</pre>\n";

Good suggestion!  It saves a lot of keystrokes, for sure.

-Keith Constable

More information about the wp-hackers mailing list