使 var_dump 看起来很漂亮 [英] Make var_dump look pretty
问题描述
我有一个简单的 $_GET[]
查询变量集,用于在从数据库中提取查询时显示测试数据.
<?php if($_GET['test']): ?><div id="test" style="padding: 24px; background: #fff; text-align: center;"><表格><tr style="font-weight: bold;"><td>MLS</td></tr><tr><td><?php echo KEY;?></td></tr><tr style="font-weight: bold;"><td>QUERY</td></tr><tr><td><?php echo $data_q;?></td></tr><tr style="font-weight: bold;"><td>DATA</td></tr><tr><td><?php var_dump($data);?></td></tr>
<?php endif;?>
当我执行 var_dump
时,正如预期的那样,这个大数组字符串全部混在一起.有没有办法至少为此添加换行符或以更具可读性的方式显示 var_dump
?我愿意接受关于在发布后处理字符串的 jQuery 建议.
我真的很喜欢 var_export()
.如果您喜欢可复制/粘贴的代码,请尝试:
echo ''.var_export($data, true) .'</pre>';
或者甚至像这样的颜色语法突出显示:
highlight_string("<?php\n\$data =\n" .var_export($data, true) .";\n?>");
你可以用 print_r()
做同样的事情.对于 var_dump()
,您只需要添加
标签:echo '
';var_dump($data);echo '</pre>';I have a simple
$_GET[]
query var set for showing testing data when pulling down queries from the DB.<?php if($_GET['test']): ?> <div id="test" style="padding: 24px; background: #fff; text-align: center;"> <table> <tr style="font-weight: bold;"><td>MLS</td></tr> <tr><td><?php echo KEY; ?></td></tr> <tr style="font-weight: bold;"><td>QUERY</td></tr> <tr><td><?php echo $data_q; ?></td></tr> <tr style="font-weight: bold;"><td>DATA</td></tr> <tr><td><?php var_dump($data); ?></td></tr> </table> </div> <?php endif; ?>
When I do
var_dump
, as expected it's this big array string that is all smushed together. Is there a way to add in line breaks at least for this or display thevar_dump
in a way that's more readable? I'm open to jQuery suggestions on manipulating the string after it's posted.解决方案I really love
var_export()
. If you like copy/paste-able code, try:echo '<pre>' . var_export($data, true) . '</pre>';
Or even something like this for color syntax highlighting:
highlight_string("<?php\n\$data =\n" . var_export($data, true) . ";\n?>");
You can do the same with
print_r()
. Forvar_dump()
you would just need to add the<pre>
tags:echo '<pre>'; var_dump($data); echo '</pre>';
这篇关于使 var_dump 看起来很漂亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!