Page 1 of 2

print_r alternative???

Posted: Sat Mar 18, 2006 6:15 pm
by alex.barylski
I love that function, but it's output is so mangled...

I use it extensively when dumping trees, but following an elements path is sometimes quite daunting...

It would be nice if there was a function which formatted a tree in the way a tree should be formatted???

Know of anything?

Cheers :)

Posted: Sat Mar 18, 2006 6:20 pm
by feyd
have an example of how a tree is supposed to be rendered?

Posted: Sat Mar 18, 2006 6:21 pm
by Buddha443556
You are?

Code: Select all

<pre><?php print_r($_SERVER); ?></pre>
Check the snippets I vaguely remember someone posting something.

Posted: Sat Mar 18, 2006 8:22 pm
by alex.barylski
Buddha443556 wrote:You are?

Code: Select all

<pre><?php print_r($_SERVER); ?></pre>
Check the snippets I vaguely remember someone posting something.
Nice :P

Thats perfect...thanks a million man...

Posted: Sat Mar 18, 2006 8:23 pm
by alex.barylski
feyd wrote:have an example of how a tree is supposed to be rendered?
Basically what buddha suggested is what I was after :)

Posted: Sat Mar 18, 2006 8:57 pm
by Chris Corbyn
It's output is already in a nested fashion :? You just need to view the source (or use <pre> like Buddha did) ;)

Posted: Sun Mar 19, 2006 2:56 am
by alex.barylski
d11wtq wrote:It's output is already in a nested fashion :? You just need to view the source (or use <pre> like Buddha did) ;)
I had seen examples of print_r but never dawned on me to use PRE I always just dumped to screen as is...but PRE does the trick nicely :)

Posted: Sun Mar 19, 2006 10:32 am
by John Cartwright
go to view source like d11 suggested, so real need for a <pre> tag, although you can make your own print_r function

Code: Select all

function dump($array) {
   echo '<pre>';
   print_r($array);
   echo '</pre>';
}

Posted: Sun Mar 19, 2006 10:44 am
by d3ad1ysp0rk
Yeah, you wouldn't imagine the amount of times I type

Code: Select all

echo "<pre>" . print_r($_POST,true) . "</pre>";
each day. :roll:

Posted: Sun Mar 19, 2006 12:11 pm
by alex.barylski
Jcart wrote:go to view source like d11 suggested, so real need for a <pre> tag, although you can make your own print_r function

Code: Select all

function dump($array) {
   echo '<pre>';
   print_r($array);
   echo '</pre>';
}
Thats exactly what i've done :)

Posted: Mon Mar 20, 2006 5:20 am
by Grim...
Mine is a little different ;)

Code: Select all

function dump ($v)
{
    print "<pre>";
    if (!$v)
    {
        print_r ($_GET);
        $v = $_POST;
    }
    print_r ($v);
    print "</pre>";
}
I'm that lazy ;)

Posted: Mon Mar 20, 2006 12:53 pm
by Chris Corbyn
I always include a dump() method in my objects :)

If you wanted to see a print_r() example that handles the recursion there is indeed one in snippets but it's for JavaScript since JS doesn't have anything like that by default :)

Posted: Mon Mar 20, 2006 6:47 pm
by Jenk

Code: Select all

<?php

echo nl2br(print_r($array, true));

?>

Posted: Mon Mar 20, 2006 9:44 pm
by d3ad1ysp0rk
nl2br won't keep the tabs the correct way afaik, so arrays inside arrays become really messy.

Posted: Mon Mar 20, 2006 9:49 pm
by feyd
when I need to do a variable dump on page, it'll often be done like this:

Code: Select all

echo '<pre>' . __FILE__ . '(' . __LINE__ . ")\n" . htmlentities(var_export($var, true), ENT_QUOTES, 'UTF-8') . '</pre>';
but that's usually when I go into shotgun-mode on finding a bug. :)