PrettyPrint for PHP

As I get better at coding PHP, I’m starting to realize the real power of being able to debug things quickly on the fly. I’m also a great believer in making things look good at the same time.

php-icon

So I was looking for a way to output an XML file in a debug window like other languages in a nice way. In the past I had always just var_dump, or echo’d the variable but this can end up pretty ugly to say the least on long input streams.

So I found out about print_r and the <pre> tag and made a small function.

// Function to show an array nicely
function prettyPrint($pretty) {
    print "<pre>";
    print_r($pretty);
    print "</pre>";
return $pretty;
}

All you need to do now is something like this in your main code page

$xml = simplexml_load_file('test.xml');
prettyPrint($xml);

Thats it! A nice prettyPrint function for PHP 🙂

Source: http://stackoverflow.com/questions/5393085/display-an-array-in-a-readable-hierarchical-format

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s