Making PHP var_dump() values display one line per value

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"

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

echo '<pre>' , var_dump($variable) , '</pre>';

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:

echo "<pre>", print_r($data, 1), "</pre>";

For var_dump():

echo "<pre>", var_dump($data), "</pre>";

I use the above with PHP Storm. I have set it as a pr tab command.


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.


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?


var_export will give you a nice output. Examples from the docs:

$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',
  ),
)