Given the following php5 code that output a gigantuous amount of difficult to read code:
<?=var_dump($_SERVER);?>
<?=print_r($GLOBALS); ?>
Question: how to make the output more human-readable? e.g. houw to but every "item" on a new line?
Given the following php5 code that output a gigantuous amount of difficult to read code:
<?=var_dump($_SERVER);?>
<?=print_r($GLOBALS); ?>
Question: how to make the output more human-readable? e.g. houw to but every "item" on a new line?
You can just wrap a pre-element around it:
<pre><?php var_dump($_SERVER); ?></pre>
<pre><?php print_r($GLOBALS); ?></pre>
Also note that <?=
requires short_open_tags
to be set to true (which is false in newer versions of php)
On your development environment, you should install the Xdebug extension.
Amongst other useful features (such as a debugger !), it'll get you nicer var_dump()
:
For example, here's a screenshot of the beggining of the output I get for var_dump($_SERVER);
:
(source: pascal-martin.fr)
You can use <pre>
tag to format the output
<pre><?=print_r($GLOBALS); ?></pre>
Like everyone else mentioned, you can wrap that in <pre>
tags to make it readable. I usually have the following 2 functions in my code at all times. Used as utility functions, inspired by cake.
function pr() {
$vars = func_get_args();
echo '<pre>';
foreach ($vars as $var) {
print_r($var);
}
echo '</pre>';
}
function prd() { //dies after print
$vars = func_get_args();
echo '<pre>';
foreach ($vars as $var) {
print_r($var);
}
echo '</pre>';
die();
}
Apart from the <pre>
trick, you can try using dbug
Makes things much nicer and clearer: dBug
the previous answers suggest good solution, but if you want more control on the output you can run a loop over the arrays.
$_SERVER and $_GLOBALS are arrays, so you can do
foreach($_SERVER as $key=>$value){
echo $key . ' is ' . $value . '<br />' . PHP_EOL;
}
you can also add if statements to ignore some items in $_SERVER/$_GLOBALS
" tag. Second, it's no good for multidimensional arrays. Of course, you can put nested foreach loops, but how far are you willing to go for something that can be done better using– Sylverdrag Apr 10 '11 at 14:19? And if you are going to make it pretty, you really should consider using dbug
/var_dump() without using debug. this particular example wont work for multi-dimensional arrays, but $_SERVER is one-dimensional and with little edits it can also support multi-dimensional arrays– gilsilas Apr 11 '11 at 09:19
? Surely you jest. If you need a quick look in the array, nothing is easier than– Sylverdrag Apr 11 '11 at 12:16. If you are going to make things pretty, use a library like dBug. If you need a closer look, xdebug is it (but a pain to install/configure). There is no case where writing your own loops is easier. Not to mention that it looks like regular code, and you will occasionally forget why it's there. Fast & clean, pre. Pretty, dBug. Detailled, xdebug. Unduly complicated, write your own dodgy version of dBug every time. There, it fits a niche.
.
foreach($_SERVER as $key => $value){
echo "<b>$key:</b> $value<br>\n";
}
Note that your output being gigantic only because you're printing out the contents of $GLOBALS variable, which being completely useless for you.