PHP 中更漂亮/信息更丰富的 Var_dump 替代方案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2141585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A more pretty/informative Var_dump alternative in PHP?
提问by raveren
Every decent PHP programmer has a print_ror var_dumpwrapper they use, love and assign shortcut keys to, why don't we share our favourite ones.
每个体面的 PHP 程序员都有一个print_r或var_dump他们使用的包装器,喜欢并为其分配快捷键,我们为什么不分享我们最喜欢的。
采纳答案by raveren
A full year of time and labor after asking this, I've finally open sourced my version of var_dump, Kint. Read about it in the project page, or directly in github.
问这个问题后,我花了整整一年的时间和精力,终于开源了我的 var_dump 版本,Kint。在项目页面或直接在 github 中阅读它。
Here's a screenshot:
这是一个屏幕截图:


Sorry for the plug :)
对不起插头:)
EDIT:I'd just like to remind the commenters, that this is not a support forum, if you're having problems/want a feature, please file an issue. Support requesting comments will be flagged for deletion.
编辑:我只是想提醒评论者,这不是一个支持论坛,如果您遇到问题/想要一个功能,请提出问题。请求评论的支持将被标记为删除。
回答by Pascal MARTIN
My prefered on is the var_dumpfunction, as provided by the Xdebug extension: just install the extension (easy, both on windows and Linux), and var_dumpgets a better output :
我上者优先的是var_dump功能,如Xdebug扩展提供:刚安装扩展(容易,无论是在Windows和Linux) ,并var_dump得到一个更好的输出:
- better formating
- HTML
- colors
- and you have options to tune how much informations should be displayed
- 更好的成型
- HTML
- 颜色
- 并且您可以选择调整应该显示多少信息
And a quick screenshot :
和一个快速截图:
And, of course, Xdebug brings loads of other usefull stuff, like remote debugging (i.e. graphical debugging of your PHP application, in Eclipse PDT for instance), profiling, ...
而且,当然,Xdebug 带来了许多其他有用的东西,如远程调试(即 PHP 应用程序的图形调试,例如在 Eclipse PDT 中)、分析,...
回答by nice ass
回答by Guillermo Phillips
Here's mine, which I use inline, very useful:
这是我的,我使用内联,非常有用:
$pretty = function($v='',$c=" ",$in=-1,$k=null)use(&$pretty){$r='';if(in_array(gettype($v),array('object','array'))){$r.=($in!=-1?str_repeat($c,$in):'').(is_null($k)?'':"$k: ").'<br>';foreach($v as $sk=>$vl){$r.=$pretty($vl,$c,$in+1,$sk).'<br>';}}else{$r.=($in!=-1?str_repeat($c,$in):'').(is_null($k)?'':"$k: ").(is_null($v)?'<NULL>':"<strong>$v</strong>");}return$r;};
echo $pretty($some_variable);
回答by Pekka
You are looking for Krumo(Warning, Chrome alerts for Malware).
您正在寻找Krumo(警告,Chrome 恶意软件警报)。
To put it simply, Krumo is a replacement for print_r() and var_dump(). By definition Krumo is a debugging tool (initially for PHP4/PHP5, now for PHP5 only), which displays structured information about any PHP variable.
简单地说,Krumo 是 print_r() 和 var_dump() 的替代品。根据定义,Krumo 是一个调试工具(最初用于 PHP4/PHP5,现在仅用于 PHP5),它显示有关任何 PHP 变量的结构化信息。
回答by Johrn
- It writes the logging information out using headers, so it doesn't break AJAX.
- Gives you a very nice graphical representation of the variables/objects you log
- Can display file name and line number where each log statement occurs
- Easy to use in your project, either with a procedural or object-oriented API
- 它使用标头写出日志信息,因此它不会破坏 AJAX。
- 为您记录的变量/对象提供非常漂亮的图形表示
- 可以显示每个日志语句出现的文件名和行号
- 易于在您的项目中使用,无论是程序化的还是面向对象的 API
回答by Tim
My (partial) solution for this is the simply add a function like this (using Google Chrome):
我的(部分)解决方案是简单地添加一个这样的功能(使用谷歌浏览器):
<?
function console_dump($value)
{
?>
<script>
console.log(<? echo json_encode($value); ?>);
</script>
<?
}
?>
Press Ctrl + Shift + J (opens up the console), and you can find there the JSON structure. Even more useful for a pretty print of JSON responses ofcourse.
按 Ctrl + Shift + J(打开控制台),您可以在那里找到 JSON 结构。对于 JSON 响应的漂亮打印当然更有用。
回答by Anthony Scaife
A complete example of what I use ...
我使用的完整示例...
<pre>
<?php
//*********** Set up some sample data
$obj = new stdClass;
$obj->a=123;
$obj->pl=44;
$obj->l=array(31,32);
$options = array(
'Orchestra'=>array(1=>'Strings', 8=>'Brass', 9=>$obj, 3=>'Woodwind', 16=>'Percussion'),
2=>'Car',
4=>'Bus',
'TV'=>array(21=>'Only Fools', 215=>'Brass Eye', 23=>'Vic Bob',44=>null, 89=>false));
//*********** Define the function
function dump($data, $indent=0) {
$retval = '';
$prefix=\str_repeat(' | ', $indent);
if (\is_numeric($data)) $retval.= "Number: $data";
elseif (\is_string($data)) $retval.= "String: '$data'";
elseif (\is_null($data)) $retval.= "NULL";
elseif ($data===true) $retval.= "TRUE";
elseif ($data===false) $retval.= "FALSE";
elseif (is_array($data)) {
$retval.= "Array (".count($data).')';
$indent++;
foreach($data AS $key => $value) {
$retval.= "\n$prefix [$key] = ";
$retval.= dump($value, $indent);
}
}
elseif (is_object($data)) {
$retval.= "Object (".get_class($data).")";
$indent++;
foreach($data AS $key => $value) {
$retval.= "\n$prefix $key -> ";
$retval.= dump($value, $indent);
}
}
return $retval;
}
//*********** Dump the data
echo dump($options);
?>
</pre>
Outputs ...
输出...
Array (4)
[Orchestra] = Array (5)
| [1] = String: 'Strings'
| [8] = String: 'Brass'
| [9] = Object (stdClass)
| | a -> Number: 123
| | pl -> Number: 44
| | l -> Array (2)
| | | [0] = Number: 31
| | | [1] = Number: 32
| [3] = String: 'Woodwind'
| [16] = String: 'Percussion'
[2] = String: 'Car'
[4] = String: 'Bus'
[TV] = Array (5)
| [21] = String: 'Only Fools'
| [215] = String: 'Brass Eye'
| [23] = String: 'Vic Bob'
| [44] = NULL
| [89] = FALSE
回答by jClark
I've been using dBug, which emulates Coldfusion's awesome cfdumptag:
我一直在使用 dBug,它模拟了 Coldfusion 的超棒cfdump标签:
回答by Damon
Here is mine:
这是我的:
class sbwDebug
{
public static function varToHtml($var = '', $key = '')
{
$type = gettype($var);
$result = '';
if (in_array($type, ['object', 'array'])) {
$result .= '
<table class="debug-table">
<tr>
<td class="debug-key-cell"><b>' . $key . '</b><br/>Type: ' . $type . '<br/>Length: ' . count($var) . '</td>
<td class="debug-value-cell">';
foreach ($var as $akey => $val) {
$result .= sbwDebug::varToHtml($val, $akey);
}
$result .= '</td></tr></table>';
} else {
$result .= '<div class="debug-item"><span class="debug-label">' . $key . ' (' . $type . '): </span><span class="debug-value">' . $var . '</span></div>';
}
return $result;
}
}
Styled with:
风格:
table.debug-table {
padding: 0;
margin: 0;
font-family: arial,tahoma,helvetica,sans-serif;
font-size: 11px;
}
td.debug-key-cell {
vertical-align: top;
padding: 3px;
border: 1px solid #AAAAAA;
}
td.debug-value-cell {
vertical-align: top;
padding: 3px;
border: 1px solid #AAAAAA;
}
div.debug-item {
border-bottom: 1px dotted #AAAAAA;
}
span.debug-label {
font-weight: bold;
}

