3

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?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Sam
  • 15,254
  • 25
  • 90
  • 145

7 Answers7

5

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)

halfdan
  • 33,545
  • 8
  • 78
  • 87
3

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() :

  • colors
  • formating


For example, here's a screenshot of the beggining of the output I get for var_dump($_SERVER); :

     
(source: pascal-martin.fr)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • +1 to this. Also the super helpful stacktrace the moment something goes wrong. Debugging is so easy! – JohnP Apr 09 '11 at 09:46
2

You can use <pre> tag to format the output

<pre><?=print_r($GLOBALS); ?></pre>
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
2

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();
}
JohnP
  • 49,507
  • 13
  • 108
  • 140
2

Apart from the <pre> trick, you can try using dbug

Makes things much nicer and clearer: dBug

Sylverdrag
  • 8,898
  • 5
  • 37
  • 54
1

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

gilsilas
  • 1,441
  • 2
  • 15
  • 24
  • 1
    Not a really good idea, really. First, it's too much code compared to a simple "
    " 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 
    ? And if you are going to make it pretty, you really should consider using dbug
    – Sylverdrag Apr 10 '11 at 14:19
  • it's great idea if you want something simple, portable and more pretty then
    /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
  • More simple, portable and pretty than
    ? Surely you jest. If you need a quick look in the array, nothing is easier than 
    . 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.
    – Sylverdrag Apr 11 '11 at 12:16
1
  1. It's not whatever "server headers" but regular arrays.
  2. To output array contents, a programmer usually makes use of a loop, and then format output in the manner they wish:

.

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345