ajax pre var_dump,debugging - A more pretty/informative Var_dump alternative in PHP? - Stack Overflo...

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 = '

preDump: '.count($args).' Parameters Found.';

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 = '

preDump: [ERROR]

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...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值