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条回答
闹够了就滚
2楼-- · 2019-03-08 18:06

For devs needing something that works in the view source and the CLI, especially useful when debugging unit tests.

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)
  }
}
查看更多
疯言疯语
3楼-- · 2019-03-08 18:07

If you got XDebug installed, you can use it's var_dump replacement. Quoting:

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.

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.

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?

查看更多
ら.Afraid
4楼-- · 2019-03-08 18:07

I did a similar solution. I've created a snippet to replace 'vardump' with this:

foreach ($variable as $key => $reg) {
    echo "<pre>{$key} => '{$reg}'</pre>";
}
var_dump($variable);die;

Ps: I'm repeating the data with the last var_dump to get the filename and line

So this: enter image description here Became this: enter image description here

Let me know if this will help you.

查看更多
一夜七次
5楼-- · 2019-03-08 18:08

Use output buffers: 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 dump to <br> elements, e.g.

ob_start();
var_dump($_SERVER) ;
echo nl2br(ob_get_clean());
查看更多
地球回转人心会变
6楼-- · 2019-03-08 18:10

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_export to return the result as a value.

查看更多
何必那么认真
7楼-- · 2019-03-08 18:13

Yes, try wrapping it with <pre>, e.g.:

echo '<pre>' , var_dump($variable) , '</pre>';
查看更多
登录 后发表回答