[wp-hackers] debugging with print_r
DD32
wordpress at dd32.id.au
Sun Sep 16 07:30:44 GMT 2007
Heh, I spotted this function in WP the other day:
wp-includes/compat.php:
function printr($var, $do_not_echo = false) {
// from php.net/print_r user contributed notes
ob_start();
print_r($var);
$code = htmlentities(ob_get_contents());
ob_clean();
if (!$do_not_echo) {
echo "<pre>$code</pre>";
}
ob_end_clean();
return $code;
}
:)
IMHO though, If you develop locally then you *really* should install xDebug, I have its profiler running whenever i'm developing a new application, and its great to locate what statement is taking up those precious seconds in a long loop.
But its greatst function is the var_dump override:
http://xdebug.org/docs/display (See "The Results", That output there is directly from var_dump with xdebug, or any of those examples on that page)
The other nice functionality is the stacktrace on errors:
http://xdebug.org/docs/stack_trace
(Infact, I didnt know you could set it to show the passed parametres either, Thats something i'm going to enable now, it'll be much easier to debug why a function fatally dies sometimes)
theres a PHP xdebug module available for windows pre-compiled, Pecl has it for linux i think..
A PHP alternative some people like is dBug:
http://www.corephp.co.uk/archives/28-dBug-var_dump-with-style!.html
D
On Sat, 15 Sep 2007 05:00:54 +1000, Ozh <ozh at planetozh.com> 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
More information about the wp-hackers
mailing list