print_r alternative???

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

print_r alternative???

Post 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 :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

have an example of how a tree is supposed to be rendered?
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

You are?

Code: Select all

<pre><?php print_r($_SERVER); ?></pre>
Check the snippets I vaguely remember someone posting something.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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...
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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) ;)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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>';
}
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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:
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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 ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

<?php

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

?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

nl2br won't keep the tabs the correct way afaik, so arrays inside arrays become really messy.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. :)
Post Reply