php 使 var_dump 看起来很漂亮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19816438/
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
Make var_dump look pretty
提问by Plummer
I have a simple $_GET[]
query var set for showing testing data when pulling down queries from the DB.
我有一个简单的$_GET[]
查询变量集,用于在从数据库中提取查询时显示测试数据。
<?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 the var_dump
in a way that's more readable? I'm open to jQuery suggestions on manipulating the string after it's posted.
当我这样做时var_dump
,正如预期的那样,这个大数组字符串全部混在一起。有没有办法至少为此添加换行符或var_dump
以更具可读性的方式显示?我愿意接受关于在发布后处理字符串的 jQuery 建议。
回答by AbraCadaver
I really love var_export()
. If you like copy/paste-able code, try:
我真的很爱var_export()
。如果您喜欢可复制/粘贴的代码,请尝试:
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()
. For var_dump()
you would just need to add the <pre>
tags:
您可以对print_r()
. 因为var_dump()
你只需要添加<pre>
标签:
echo '<pre>';
var_dump($data);
echo '</pre>';
回答by Latheesan
回答by Wael Assaf
Use preformatted HTML element
使用预先格式化的 HTML 元素
echo '<pre>';
var_dump($data);
echo '</pre>';
回答by Muhammad Awais
I have make an addition to @AbraCadaver answers. I have included a javascript script which will delete php starting and closing tag. We will have clean more pretty dump.
我对@AbraCadaver 的回答做了补充。我已经包含了一个 javascript 脚本,它将删除 php 开始和结束标记。我们将有干净的更漂亮的转储。
May be somebody like this too.
可能也是这样的人。
function dd($data){
highlight_string("<?php\n " . var_export($data, true) . "?>");
echo '<script>document.getElementsByTagName("code")[0].getElementsByTagName("span")[1].remove() ;document.getElementsByTagName("code")[0].getElementsByTagName("span")[document.getElementsByTagName("code")[0].getElementsByTagName("span").length - 1].remove() ; </script>';
die();
}
Result before:
之前的结果:
Result After:
结果之后:
Now we don't have php starting and closing tag
现在我们没有 php 开始和结束标签
回答by WoodrowShigeru
If it's "all smushed together" you can often give the ol' "view source code" a try. Sometimes the dumps, messages and exceptions seem like they're just one long string when it turns out that the line breaks simply don't show. Especially XML trees.
如果它“全部混在一起”,您通常可以尝试一下“查看源代码”。有时转储、消息和异常似乎只是一个长字符串,但事实证明换行符根本不显示。尤其是 XML 树。
Alternatively, I've once created a small little tool called InteractiveVarDumpfor this very purpose. It certainly has its limits but it can also be very convenient sometimes. Even though it was designed with PHP 5 in mind.
或者,我曾经为此创建了一个名为InteractiveVarDump 的小工具。它当然有其局限性,但有时也非常方便。尽管它的设计考虑到了 PHP 5。
回答by Mr.Hosseini
function var_view($var)
{
ini_set("highlight.keyword", "#a50000; font-weight: bolder");
ini_set("highlight.string", "#5825b6; font-weight: lighter; ");
ob_start();
highlight_string("<?php\n" . var_export($var, true) . "?>");
$highlighted_output = ob_get_clean();
$highlighted_output = str_replace( ["<?php","?>"] , '', $highlighted_output );
echo $highlighted_output;
die();
}
回答by zpert
I don't seem to have enough rep to close this as a duplicate, but it is one if someone else can do that. I posted the same thing over at A more pretty/informative Var_dump alternative in PHP?but for the sake of saving time, I'll copy/paste it here too:
我似乎没有足够的代表来关闭它作为重复,但如果其他人可以做到这一点,它就是一个。我在 PHP 中更漂亮/信息更丰富的 Var_dump 替代方案中发布了同样的内容?但为了节省时间,我也会在这里复制/粘贴:
I had to add another answer here because I didn't really want to go through the steps in the other solutions. It is extremely simple and requires no extensions, includes etc and is what I prefer. It's very easy and very fast.
我不得不在这里添加另一个答案,因为我真的不想完成其他解决方案中的步骤。它非常简单,不需要扩展,包括等,是我更喜欢的。这非常容易且非常快。
First just json_encode the variable in question:
首先只是 json_encode 有问题的变量:
echo json_encode($theResult);
Copy the result you get into the JSON Editor at http://jsoneditoronline.org/just copy it into the left side pane, click Copy > and it pretty prints the JSON in a really nice tree format.
将您获得的结果复制到http://jsoneditoronline.org/的 JSON 编辑器中,只需将其复制到左侧窗格中,单击复制 >,它就会以非常漂亮的树格式打印 JSON。
To each their own, but hopefully this helps some others have one more nice option! :)
每个人都有自己的,但希望这可以帮助其他人有一个更好的选择!:)
回答by Sascha Eisner
You could use this one debugVar()
instead of var_dump()
你可以用这个debugVar()
代替var_dump()
Check out: https://github.com/E1NSER/php-debug-function
回答by Arno
Here is my function to have a pretty var_dump. Combined with Xdebug, it helps a lot to have a better view of what we are dumping.
这是我拥有漂亮 var_dump 的函数。结合Xdebug,它有助于更好地了解我们正在转储的内容。
I improved a bit the display of Xdebug (give some space around, separator between values, wrap long variables, etc).
我改进了 Xdebug 的显示(在周围留出一些空间、值之间的分隔符、包装长变量等)。
When you call the function, you can set a title, a background, a text color to distinguish all your var_dump in a page.
调用该函数时,您可以设置标题、背景、文本颜色来区分页面中的所有 var_dump。
Or not ;)
或不 ;)
/**
* Pretty var_dump
* Possibility to set a title, a background-color and a text color
*/
function dump($data, $title="", $background="#EEEEEE", $color="#000000"){
//=== Style
echo "
<style>
/* Styling pre tag */
pre {
padding:10px 20px;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
white-space: -pre-wrap;
white-space: -o-pre-wrap;
word-wrap: break-word;
}
/* ===========================
== To use with XDEBUG
=========================== */
/* Source file */
pre small:nth-child(1) {
font-weight: bold;
font-size: 14px;
color: #CC0000;
}
pre small:nth-child(1)::after {
content: '';
position: relative;
width: 100%;
height: 20px;
left: 0;
display: block;
clear: both;
}
/* Separator */
pre i::after{
content: '';
position: relative;
width: 100%;
height: 15px;
left: 0;
display: block;
clear: both;
border-bottom: 1px solid grey;
}
</style>
";
//=== Content
echo "<pre style='background:$background; color:$color; padding:10px 20px; border:2px inset $color'>";
echo "<h2>$title</h2>";
var_dump($data);
echo "</pre>";
}
回答by Moris Finkel
The best what and easiest way to get nice var_dump is use xDebug (must have for any php dev) Debian way install
获得好 var_dump 的最好和最简单的方法是使用 xDebug(任何 php 开发者都必须拥有)Debian 方式安装
In console: apt-get install php-xdebug
after that you should open php.ini (depends on which stack you use) for it's /etc/php/7.0/fpm/php.ini
在控制台中:apt-get install php-xdebug
之后你应该打开 php.ini(取决于你使用的堆栈)因为它是 /etc/php/7.0/fpm/php.ini
Search for display_errors
搜索 display_errors
set same -> display_errors = On
设置相同-> display_errors = On
Check html_errors
in same file a little bit below, it's also must be On
html_errors
在下面稍微检查同一个文件,它也必须是On
Save and exit
保存并退出
After open /etc/php/7.0/fpm/conf.d/20-xdebug.ini
打开后 /etc/php/7.0/fpm/conf.d/20-xdebug.ini
And add to the end: ``` xdebug.cli_color=1
并添加到最后:``` xdebug.cli_color=1
``` Save and exit.
``` 保存退出。
A lot other available option and documentation for xdebug can be founded here.
可以在此处找到 xdebug 的许多其他可用选项和文档。
Good luck and Have Fun !!!
祝好运并玩得开心点 !!!