Output an object in PHP
March 26, 2006 11:01 PM   Subscribe

I remember there's some kind of output function in PHP that will print an object or array broken down into its parts.

This is insanely useful, yet I keep forgetting the name of the function.
posted by RobotHero to Computers & Internet (7 answers total)
 
It's the print_r() function.

<?php

$ar = array('mefi'=>'metafilter','axmefi'=>'ask metafilter','more'=>array('metatalk','projects','wiki');
print_r($ar);

?>


will output
Array
(
    [mefi] => metafilter
    [axmefi] => ask metafilter
    [more] => Array
        (
            [0] => metatalk
            [1] => projects
            [2] => wiki
        )
)

posted by charmston at 11:14 PM on March 26, 2006


Please note to achieve the formatting charmston displays, at least when printing to a browser, you should wrap the print_r() function in <pre> &lt/pre>

Otherwise, it's less readable than you might like.
posted by disillusioned at 12:13 AM on March 27, 2006


or var_dump($variable1[, $variable2 ...]);

There's a distinction between this and print_r, but it's one of those things I've been content to forget, because it doesn't make much odds.
posted by NinjaPirate at 1:26 AM on March 27, 2006


It's been years since I lifted a code to do this for my old site, but doesn't Explode do that? Just suggesting because I didn't see it already.
posted by Brainy at 6:24 AM on March 27, 2006


No, explode just takes a string and splits it into an array. It doesn't actually print anything.
posted by matthewr at 6:29 AM on March 27, 2006


Obviously you need a mnemonic.

"The function's called print_r(), eh?"
posted by DrJohnEvans at 7:05 AM on March 27, 2006


var_dump prints very detailed output. It will give you the types for every variable. print_r's output is more nicely formatted, but can be ambiguous. For example:

print_r:  hello => world
var_dump: hello => string(5) "world"

print_r:  empty =>
var_dump: empty => null
    or    empty => string(0) ""

posted by Khalad at 7:16 AM on March 27, 2006


« Older Okay, voice recognition technically works, but...   |   The King's jumpsuits Newer »
This thread is closed to new comments.