Debugging Data in PHP
by Guillermo A. FisherMy buddy John C. Bland II mentioned some debugging functions I wrote up a while back while we worked together on some code for Elevate (an amazing company, by the way). I’d first seen something very similar to his dump() function at a previous job (although it was named debug() there), so I felt right at home using his version. I found myself following up calls to the function with the die and exit constructs on several occasions, however; and I also needed to know about data types every now and then. So I made some changes and wrote up another function. Since then, I’ve combined those 2 functions into one compact function that I now use everyday, no matter 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 parameter order. I pasted an old version the first time lol. Sorry!
Your Feedback Civil & constructive, please.
I am gonna snatch this one here for my CakePHP debuggings! Thanks in advance!