views:

3445

answers:

8

I'm fixing some PHP scripts and I'm missing ruby's pretty printer. i.e.

require 'pp'
arr = {:one => 1}
pp arr

will output {:one => 1}. This even works with fairly complex objects and makes digging into an unknown script much easier. Is there some way to duplicate this functionality in PHP?

+3  A: 

How about print_r?

http://www.php.net/print_r

Turnor
+10  A: 

Both print_r() and var_dump() will output visual representations of objects within PHP.

$arr = array('one' => 1);
print_r($arr);
var_dump($arr);
Andrew Moore
If you install the XDebug extension, the var_dump becomes an even prettier printer.
Alan Storm
Thank you, turns out it's just an issue of naming! I say po ta to you say pa_ta_to.
Aaron Lee
To make it look even nicer in a browser use:echo "<pre>";print_r($arr);echo "</pre>";
Domenic
To Domenic's point just wrap it: function pr($array = null) { print "<pre><code>" . print_r($array) . "</code></pre>"; }
Darren Newton
**@darren_n:** `print_r()` automatically outputs and doesn't return anything (unless its second parameter is `true`), so you can't concatenate to another string. Use the following instead: `function pr($var) { print '<pre>'; print_r($var); print '</pre>'; }`
Andrew Moore
+3  A: 

For simplicity, print_r() and var_dump() can't be beat. If you want something a little fancier or are dealing with large lists and/or deeply nested data, Krumo will make your life much easier - it provides you with a nicely formatted collapsing/expanding display.

Sean McSomething
+1 for Krumo. A very nice tool!
Gary Willoughby
I've used it before - and it's pretty cool )
Malachi
A: 
error_log(print_r($variable,true));

to send to syslog or eventlog for windows

Question Mark
+1  A: 

If you're doing more debugging, Xdebug is essential. By default it overrides var_dump() with it's own version which displays a lot more information than PHP's default var_dump().

There's also Zend_Debug.

raspi
Blarg. Xdebug's var dump _sucks_ because it outputs HTML... Oh yeah, looks _fantastic_ on a CLI test.
jason
+1  A: 

For PHP, you can easily take advantage of HTML and some simple recursive code to make a pretty representation of nested arrays and objects.

function pp($arr){
    $retStr = '<ul>';
    if (is_array($arr)){
        foreach ($arr as $key=>$val){
            if (is_array($val)){
                $retStr .= '<li>' . $key . ' => ' . pp($val) . '</li>';
            }else{
                $retStr .= '<li>' . $key . ' => ' . $val . '</li>';
            }
        }
    }
    $retStr .= '</ul>';
    return $retStr;
}

This will print the array as a list of nested HTML lists. HTML and your browser will take care of indenting and making it legible.

Stephen Katulka
A: 

Since I found this via google searching for how to format json to make it more readable for troubleshooting.

ob_start() ;  print_r( $json ); $ob_out=ob_get_contents(); ob_end_clean(); echo "\$json".str_replace( '}', "}\n", $ob_out );
A: 

Wow, I already answered this. I really should keep better track of where I've been!

Steve K
You should have a "delete" link under this post so that you can remove the duplicate...
sth