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('<pre style="text-align:left">' . var_dump($expression) . '</pre>'); } else { echo('<pre style="text-align:left">' . print_r($expression, TRUE) . '</pre>'); } } // ------------------------------------------------------------------------ /** * 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?