使 PHP var_dump() 值每个值显示一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10116063/
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
Making PHP var_dump() values display one line per value
提问by user1320318
When I echo var_dump($_variable), I get one long, wrapping line with all varable's and values like
当我回显 var_dump($_variable) 时,我得到一个长的、包含所有变量和值的换行行,例如
["kt_login_user"]=> string(8) "teacher1" ["kt_login_id"]=> string(3) "973" ["kt_campusID"]=> string(4) "9088" ["kt_positionID"]=> string(1) "5"
Is there a way I can make each value display on its own line for ease of reading? Something like this:
有没有一种方法可以让每个值显示在自己的行上以便于阅读?像这样的东西:
["kt_login_user"]=> string(8) "teacher1"
["kt_login_id"]=> string(3) "973"
["kt_campusID"]=> string(4) "9088"
["kt_positionID"]=> string(1) "5"
回答by phirschybar
Yes, try wrapping it with <pre>, e.g.:
是的,尝试用 包裹它<pre>,例如:
echo '<pre>' , var_dump($variable) , '</pre>';
回答by Dave
I usually have a nice function to handle output of an array, just to pretty it up a bit when debugging.
我通常有一个很好的函数来处理数组的输出,只是为了在调试时稍微修饰一下。
function pr($data)
{
echo "<pre>";
print_r($data); // or var_dump($data);
echo "</pre>";
}
Then just call it
然后调用它
pr($array);
Or if you have an editor like that saves snippets so you can access them quicker instead of creating a function for each project you build or each page that requires just a quick test.
或者,如果您有一个这样的编辑器,可以保存片段,以便您可以更快地访问它们,而不是为您构建的每个项目或只需要快速测试的每个页面创建一个函数。
For print_r:
对于print_r:
echo "<pre>", print_r($data, 1), "</pre>";
For var_dump():
对于var_dump():
echo "<pre>", var_dump($data), "</pre>";
I use the above with PHP Storm. I have set it as a prtab command.
我将上述内容与 PHP Storm 一起使用。我已将其设置为pr选项卡命令。
回答by Mark
I've also researched this issue and not found the right answer. This doesn't work for me:
我也研究过这个问题,但没有找到正确的答案。这对我不起作用:
echo '<pre>' . var_dump($variable) . '</pre>';
This will not provide a nice display of the array for me, with line breaks (I'm using Firefox 31.3.0)
这不会为我提供一个很好的数组显示,带有换行符(我使用的是 Firefox 31.3.0)
However, after some experimentation, this solved the problem (notice the php is closed at first):
但是,经过一些实验,这解决了问题(注意 php 一开始是关闭的):
... ?> <pre><?php echo var_dump($variable) ?></pre> <?php ...
This solves the problem and displays a nice, easy-to-read array for me on my browser. You see how the tags are not wrapped in PHP; only the echo var_dump part is.
这解决了这个问题,并在我的浏览器上为我显示了一个漂亮的、易于阅读的数组。您会看到标签是如何不包含在 PHP 中的;只有 echo var_dump 部分是。
回答by dm03514
If you got XDebuginstalled, you can use it's var_dumpreplacement. Quoting:
如果您安装了XDebug,则可以使用它的var_dump替代品。引用:
Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes different colors for different types and places limits on the amount of array elements/object properties, maximum depth and string lengths. There are a few other functions dealing with variable display as well.
Xdebug 替换了 PHP 的 var_dump() 函数来显示变量。Xdebug 的版本包括不同类型的不同颜色,并限制了数组元素/对象属性的数量、最大深度和字符串长度。还有一些其他函数处理变量显示。
You will likely want to tweak a few of the following settings:
您可能需要调整以下一些设置:
There is a number of settings that control the output of Xdebug's modified var_dump() function: xdebug.var_display_max_children, xdebug.var_display_max_data and xdebug.var_display_max_depth. The effect of these three settings is best shown with an example. The script below is run four time, each time with different settings. You can use the tabs to see the difference.
有许多设置控制 Xdebug 修改后的 var_dump() 函数的输出:xdebug.var_display_max_children、xdebug.var_display_max_data 和 xdebug.var_display_max_depth。这三个设置的效果最好通过一个例子来展示。下面的脚本运行四次,每次都使用不同的设置。您可以使用选项卡查看差异。
But keep in mind that XDebug will significantly slow down your code, even when it's just loaded. It's not advisable to run in on production servers. But hey, you are not var_dumping on production servers anyway, are you?
但请记住,XDebug 会显着减慢您的代码,即使它刚刚加载。不建议在生产服务器上运行。但是,嘿,无论如何,您不是在生产服务器上进行 var_dumping,是吗?
回答by Tieme
var_exportwill give you a nice output. Examples from the docs:
var_export会给你一个很好的输出。文档中的示例:
$a = array (1, 2, array ("a", "b", "c"));
echo '<pre>' . var_export($a, true) . '</pre>';
Will output:
将输出:
array (
0 => 1,
1 => 2,
2 =>
array (
0 => 'a',
1 => 'b',
2 => 'c',
),
)
回答by Humberto Casta?eda
For me the right answer was
对我来说正确的答案是
echo '<pre>' . var_export($var, true) . '</pre>';
Since var_dump($var)and var_export($var)do not return a string, you have to use var_export($var, true)to force var_exportto return the result as a value.
由于var_dump($var)并且var_export($var)不返回字符串,因此您必须使用var_export($var, true)强制var_export将结果作为值返回。
回答by Stephan Weinhold
You can press Ctrl+Uto view the source code. Most Browsers will prettify the output there.
您可以按Ctrl+U查看源代码。大多数浏览器都会美化那里的输出。
var_dumpis the ugliest way to debug.
var_dump是最丑陋的调试方式。
回答by Aroddo
Use output buffers: http://php.net/manual/de/function.ob-start.php
使用输出缓冲区:http: //php.net/manual/de/function.ob-start.php
<?php
ob_start();
var_dump($_SERVER) ;
$dump = ob_get_contents();
ob_end_clean();
echo "<pre> $dump </pre>";
?>
Yet another option would be to use Output buffering and convert all the newlines in the dumpto <br>elements, e.g.
另一种选择是使用输出缓冲并将转储中的所有换行符转换为<br>元素,例如
ob_start();
var_dump($_SERVER) ;
echo nl2br(ob_get_clean());
回答by Ricky Boyce
For devs needing something that works in the view sourceand the CLI, especially useful when debugging unit tests.
对于需要在视图源代码和CLI中工作的开发人员,在调试单元测试时特别有用。
echo vd([['foo'=>1, 'bar'=>2]]);
function vd($in) {
ob_start();
var_dump($in);
return "\n" . preg_replace("/=>[\r\n\s]+/", "=> ", ob_get_clean());
}
Yields:
产量:
array(1) {
[0] => array(2) {
'foo' => int(1)
'bar' => int(2)
}
}
回答by apokryfos
Personally I like the replacement function provided by Symfony's var dumper component
我个人喜欢Symfony 的 var dumper 组件提供的替换功能
Install with composer require symfony/var-dumperand just use dump($var)
安装composer require symfony/var-dumper并使用dump($var)
It takes care of the rest. I believe there's also a bit of JS injected there to allow you to interact with the output a bit.
它负责其余的工作。我相信那里还注入了一些 JS 以允许您与输出进行一些交互。

