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 = '<div style="margin: 1em 0;"><fieldset style="display:inline-block;padding:0em 3em 1em 1em;"><legend><b>preDump: '.count($args).' Parameters Found.</b></legend>';
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 .= '<pre data-type="'.gettype($arg).'"';
switch (gettype($arg)) {
case "boolean":
case "integer":
$sb .= ' data-dump="json_encode"><p style="border-bottom:1px solid;margin:0;padding:0 0 0 1em;"><b>gettype('.gettype($arg).')</b></p><p>';
$sb .= json_encode($arg);
break;
case "string":
$sb .= ' data-dump="echo"><p style="border-bottom:1px solid;margin:0;padding:0 0 0 1em;"><b>gettype('.gettype($arg).')</b></p><p>';
$sb .= $arg;
break;
default:
$sb .= ' data-dump="var_dump"';
if (is_object($arg)) $sb .= 'data-class="'.get_class($arg).'"';
$sb .= '><p style="border-bottom:1px solid;margin:0;padding:0 0 0 1em;"><b>gettype('.gettype($arg).')';
if (is_object($arg)) $sb .= ' ['.get_class($arg).']';
$sb .= '</b></p><p>';
ob_start();
var_dump($arg);
$sb .= ob_get_clean();
if (ob_get_length()) ob_end_clean();
}
$sb .= '</p></pre>';
}
$sb .= '</fieldset></div>';
}
else {
$sb = '<div style="margin: 1em 0;"><fieldset style="display:inline-block;"><legend><b>preDump: [ERROR]</b></legend><h3>No Parameters Found</h3></fieldset></div>';
}
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.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
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 = '<script type="text/javascript">console.log("<" + new Array('.(count($args) < 10 ? '49': '48').').join("-") + "[consoleDump: '.count($args).' items]" + new Array(50).join("-") + ">"); console.log([';
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("-") + ">"); </script>';
}
else {
$sb = '<script type="text/javascript">console.log("<" + new Array(120).join("-") + ">");console.log("consoleDump: [ERROR] No Parameters Found");console.log("<" + new Array(120).join("-") + ">");</script>';
}
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' ));
<------------------------------------------------[consoleDump: 6 items]------------------------------------------------->
[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...
<----------------------------------------------------------------------------------------------------------------------->