The Schworak Site | Log In | Up One Level

Beautiful PHP dump to use in place of var_dump or print_r

PHP is pretty nice offering var_dump and print_r to let us dump the contents of a variable. The only problem is that is is abit ugly to look at and recursing into objects isn't always all that useful. Here is a solution.

Using the function is simple, you can call it without a label (as shown above) or add an extra label to the top level. The lable is handy when dumping multiple variables. You might place the name of the variable in the title bar at the first level.

If you would rather not display this information to the screen, you can capture it to a variable by passing FALSE in as the third parameter.

// dump to the screen
dump($myVariable);
dump($myVariable,"myVariable");

// dump to a variable
$myDump1=dump($myVariable,"",false);
$myDump2=dump($myVariable,"myVariable",false);

// dump to the screen and a variable
$myDump3=dump($myVariable);
$myDump4=dump($myVariable,"myVariable");

There are actually two additional parameters, but they are intended for internal use allowing this function to recurse down through all the levels of the variable you want dumped. You can examine the source code to see all the details.

Of course, you can change the CSS to match your needs or even take it out of the function all together and use your own CSS in its place.

View the source code.

Comments

Cross session variables that survive a reboot. >>
All content on this site is copyright ©2004-2021 and is not to be reproduced without prior permission.