Those fancy libraries are great ... except the overhead. If you want a simple, pretty var_dump that takes infinite parameters, try my function. It adds some simple HTML. Data attributes are added too, if you use HTML5, lower versions will just ignore them, but makes it easy to open element in browser console and get a little more info if what you see on screen is not enough.
The layout is very simple, no overhead. Provides a ton of info for each parameter including things like gettype and even class name for Object dumps (including XML). It's tried and true, I've been using it for years.
function preDump() { // use string "noEcho" to just get a string return only
$args = func_get_args();
$doEcho = TRUE; $sb;
if ($args) {
$sb = '
foreach (func_get_args() as $arg) {
if (gettype($arg) == 'string') if ($arg == 'noEcho') { $doEcho = FALSE; $sb = preg_replace('/(preDump: )[0-9]+/', 'preDump: '.(count($args)-1), $sb); continue; }
$sb .= '
switch (gettype($arg)) {
case "boolean":
case "integer":
$sb .= ' data-dump="json_encode">
gettype('.gettype($arg).')
';
$sb .= json_encode($arg);
break;
case "string":
$sb .= ' data-dump="echo">
gettype('.gettype($arg).')
';
$sb .= $arg;
break;
default:
$sb .= ' data-dump="var_dump"';
if (is_object($arg)) $sb .= 'data-class="'.get_class($arg).'"';
$sb .= '>
gettype('.gettype($arg).')';
if (is_object($arg)) $sb .= ' ['.get_class($arg).']';
$sb .= '
';
ob_start();
var_dump($arg);
$sb .= ob_get_clean();
if (ob_get_length()) ob_end_clean();
}
$sb .= '
';}
$sb .= '
}
else {
$sb = '
No Parameters Found
}
if ($doEcho) echo($sb);
return $sb;
}
And If you use Codeigniter, add it too your CI EXTREMELY SIMPLY. First, go to application/config/autoload.php and make sure the helper 'string' is on.
$autoload['helper'] = array( 'string' );
Then simply go create a file named MY_string_helper.php in application/helpers and simple insert the function in a typical if statement for existence check.
if (!function_exists('preDump')) {
function preDump() {
...
}
}
// DON'T CLOSE PHP
|OR|, if you want to take it a different direction.
The following snippet is the same as above, except will show your variables in the browser console. This can sometimes make it easier to debug sql object calls and other array and object calls where you're missing the key name or whatever.
function consoleDump() { // use string "noEcho" to just get a string return only
$args = func_get_args();
$doEcho = TRUE; $sb;
if ($args) {
$sb = '
foreach (func_get_args() as $i => $arg) {
if (gettype($arg) == 'string') if ($arg == 'noEcho') { $doEcho = FALSE; $sb = preg_replace('/(consoleDump: )[0-9]+/', 'consoleDump: '.(count($args)-1), $sb); continue; }
$sb .= '{ "type": "'.gettype($arg).'", ';
switch (gettype($arg)) {
case "boolean":
case "integer":
case "string":
$sb .= '"value": '.json_encode($arg);
break;
default:
$sb .= '"value": '.json_encode($arg);
if (is_object($arg) || is_array($arg)) $sb .= ', "count": '.json_encode(count((array)$arg));
if (is_object($arg)) $sb .= ', "objectClass": "'.get_class($arg).'"';
}
$sb .= '}';
if ($i < count($args)-1) $sb .= ', ';
}
$sb .= ']); console.log("<" + new Array(120).join("-") + ">"); ';
}
else {
$sb = '';
}
if ($doEcho) echo($sb);
return $sb;
}
Works with everything!
consoleDump($simpXMLvar, $_SESSION, TRUE, NULL, array( 'one' => 'bob', 'two' => 'bill' ), (object)array( 'one' => 'bob', 'two' => 'bill' ));
[Object, Object, Object, Object, Object, Object]
// This drops down to show your variables in JS objects, like:
0: Object
count: 4
objectClass: "SimpleXMLElement"
type: "object"
value: Object
__proto__: Object
// ...etc...