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_lo
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.
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.
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?
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());
Yes, try wrapping it with <pre>
, e.g.:
echo '<pre>' , var_dump($variable) , '</pre>';
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.