Debugging Data in PHP

by Guillermo A. Fisher

My buddy John C. Bland II men­tioned some debug­ging func­tions I wrote up a while back while we worked together on some code for Ele­vate (an amaz­ing com­pany, by the way). I’d first seen some­thing very sim­i­lar to his dump() func­tion at a pre­vi­ous job (although it was named debug() there), so I felt right at home using his ver­sion. I found myself fol­low­ing up calls to the func­tion with the die and exit con­structs on sev­eral occa­sions, how­ever; and I also needed to know about data types every now and then. So I made some changes and wrote up another func­tion. Since then, I’ve com­bined those 2 func­tions into one com­pact func­tion that I now use every­day, no mat­ter the project. Here goes:

/**
 * Prints information about an expression.
 * 
 * @param mixed $x  The expression
 * @param bool $die  OPTIONAL Whether or not to stop script execution
 * @param bool $verbose  OPTIONAL Whether or not to print verbose information
 */
function dump($x, $die = false, $verbose = false)
{
	echo '<pre>';
	if ($verbose) {
		echo var_dump($x);
	} else {
		echo print_r($x, true);
	}
	echo '</pre>';
	if ($die) {
		exit;
	}
}

EDIT: I changed the para­me­ter order. I pasted an old ver­sion the first time lol. Sorry!

Your Feedback Civil & constructive, please.

One Response to “Debugging Data in PHP

  1. Abou Kone says:

    I am gonna snatch this one here for my CakePHP debug­gings! Thanks in advance!

Leave a Reply