Reputation: 1830
I usually use var_dump()
to dump the contents of my array so as to assess the structure. However, this becomes tedious as with larger dumps, information spreads all across my 23" screen making it extremely hard to easily pick out which key matches which value.
Is there anyway to have the contents of an array dumped vertically, similar to the fashion which the PHP Manual uses:
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
I'd really like to know, as this would be a great timesaver.
Any comments/suggestions/answers will be greatly appreciated ;)!!
Upvotes: 2
Views: 135
Reputation: 12047
If you want a non archaic debugging, var_dump, echo and other inline display of data are not the best way to go, xdebug and an IDE that supports debugging (netbeans, eclipse, aptana, ...) is.
However, you can improve the readability of var_dump a lot by echoing a pre tag before it:
function dump($var)
{
echo '<pre>';
var_dump($var);
echo '</pre>';
}
(note that you can get the same sort of results by showing the source of the page instead of the html rendering)
Upvotes: 6
Reputation: 53646
Xdebug can replace the plain text output of var_dump() with nicely formatted/colorized HTML. See http://xdebug.org/docs/display
Upvotes: 0
Reputation: 23102
Personally, I use a custom debug() function and <pre>
:
function debug($val) {
if (defined('DEBUG') && DEBUG) { // So debugging won't show on live.
// Turn off debugs via debug=0 in url.
if(!@$_REQUEST['debug'] && @$_REQUEST['debug'] !== 0){ // Can see a clean page by again by just appending a url parameter.
$vals = func_get_args();
foreach($vals as $val){
echo "<pre class='debug' style='font-weight:bold;font-size:1.1em;background-color:white;color:black;position:relative;z-index:10;clear:both;max-width:980px;overflow:auto'>";
var_dump($val);
echo "</pre>";
}
}
}
}
You might also want to check into http://xdebug.org/ Xdebug, which formats var_dumps for html viewing. I've used that in the past as well.
Upvotes: 1
Reputation: 40243
Also, FirePHP can be installed and will send debug info to firebug in your browser like so: FB::log('Log message');
Upvotes: 0
Reputation: 9481
The trouble is that PHP formats the array using newline characters, not 'br' elements. And because the default Content-type
is text/html
, the browser ignores newline characters, you don't see them.
There are two solutions:
Set the content type to text/plain
:
header('Content-type: text/plain');
var_dump($some_array);
Upvotes: 0
Reputation: 1112
"view source" in your browser or
echo '<pre>'; var_dump($var); echo '</pre>';
Upvotes: 0