Making PHP var_dump() values display one line per

2019-03-08 17:26发布

When I echo var_dump($_variable), I get one long, wrapping line with all varable's and values like

["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"

14条回答
可以哭但决不认输i
2楼-- · 2019-03-08 17:52

You can press Ctrl+U to view the source code. Most Browsers will prettify the output there.

var_dump is the ugliest way to debug.

查看更多
在下西门庆
3楼-- · 2019-03-08 17:54

I didn't wanna stop using var_dump($variable);die(); and using pre tags and loops seems overkill to me, so since I am looking at the dump in a browser, I just right click the page and choose Inspect (I use Chrome). The Elements section of the Developer Tools show the variable in a very readable format.

查看更多
成全新的幸福
4楼-- · 2019-03-08 17:55

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?>");
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-03-08 17:57

Personally I like the replacement function provided by Symfony's var dumper component

Install with composer require symfony/var-dumper and just use 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.

查看更多
Emotional °昔
6楼-- · 2019-03-08 17:58

Wrap it in <pre> tags to preserve formatting.

查看更多
老娘就宠你
7楼-- · 2019-03-08 18:00

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)

However, after some experimentation, this solved the problem (notice the php is closed at first):

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

查看更多
登录 后发表回答