I’m John C Bland II

Husband, Father, Tech Author, Deacon.
Founder of Katapult Media, and full-stack polyglot developer.
Political Free Agents Podcast Host.

I create. I launch.

YouTube Channel

I post regular fun on YouTube like me playing the bass and anything else I find fun. 

Get Something Built

All project work goes through Katapult Media. Business is open. Let’s chat.

PHP Quick Tip: Pretty Debug Printing

I’ve been using this for years [circa 2002] and in helping a friend with some PHP probs I pulled it out of my code vault to share and figured I should put here.

function dump($var){
	print "<pre>" . print_r($var, true) . "</pre>";
}

This will print an array in a vertical layout as opposed to a horizontal one. Keep in mind there is a pre there [Wordpress isn’t playing nice so replace html equivalent values, lt and gt, with an actual less than/greater than symbol] so you could add a class to it so you can style your debug, float it, or do whatever.

Here is an example of using the file:

function dump($var){
	print "<pre>" . print_r($var, true) . "</pre>";
}
 
dump($_GET);

I put this code in dump.php, loaded https://localhost/dump.php?x=1&y=2&z=3, and here is the output:

Array
(
    [x] => 1
    [y] => 2
    [z] => 3
)

Two great versions came from this function by my friend Guillermo A. Fisher (@guillermoandrae):

/**
 * This function prints information about an expression.
 * Elevate specialty ;).
 * @param	mixed	expression to be dumped
 * @param	boolean	whether or not to dump detailed information
 * @return	void
 * @access	public
 */
function dump($expression, $verbose = FALSE) {
	if ($verbose) {
		echo('&lt;pre style="text-align:left"&gt;' . var_dump($expression) . '</pre&gt;');
	} else {
		echo('&lt;pre style="text-align:left"&gt;' . print_r($expression, TRUE) . '&lt;/pre&gt;');
	}
}
 
// ------------------------------------------------------------------------
 
/**
 * This function prints information about an expression and stops script 
 * execution. Another Elevate specialty.
 * @param	mixed	expression to be dumped
 * @param	boolean	whether or not to dump detailed information
 * @return	void
 * @access	public
 */
function fatal_dump($expression, $verbose = FALSE) {
	die(dump($expression, $verbose));
}

All of this came out of working with one of my favorite design firms Elevate.

I hope this helps someone. What are your ways of dumping values to debug your pages?