Last Updated: February 25, 2016
·
1.062K
· vfalconi

Helpful PHP Output

function output($str,$recursive=null)
{
    if (!$recursive)
    {
        echo '<pre><code>'.$str.'</code></pre>';
    }
    else
    {
        echo ('<pre><code>');
        print_r($str);
        echo ('</code></pre>');
    }
}

I put this function in place to make my dev process more efficient. Now, I have less code to write when I just want to see what's going on with a variable. (Inspired by "'Nicer' print_r" by @chdcxa)

5 Responses
Add your response

Great idea. I've always used
echo '<pre>'; print_r($str); echo '</pre>';
but never though to put it in a function like that.

over 1 year ago ·

I've always just used xDebug's ability to overload var_dump to do exactly the same.

I don't really see any advantage in creating custom methods for things PHP is already quite capable of doing all by itself...

over 1 year ago ·

Well, @potherca, I'm not familiar with the xDebug extension. Thanks for that, though.

For what it's worth, on the topic of non-native solutions, I've found using Chrome Logger (http://craig.is/writing/chrome-logger) really helpful, outputting things to the Chrome dev console.

over 1 year ago ·

@vfalconi I've used that one too!

Before switching from Firefox to Chrome for web development, I used to use FirePHP.
For a brief period I used Chrome Logger before going with a customized version of chrome-firephp

That way I could view the output both in Chrome and Firefox.

over 1 year ago ·

I added a shortcut to ST2 that gives me a pretty printing line, similar to what your function does, that also includes file name, and line number so I can easily find all of them when I am done debugging. See http://coderwall.com/p/3fa_pw

over 1 year ago ·