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?

7 Answers 7

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)

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)

1
  • +1 to this. Also the super helpful stacktrace the moment something goes wrong. Debugging is so easy!
    – JohnP
    Apr 9, 2011 at 9:46
2

You can use <pre> tag to format the output

<pre><?=print_r($GLOBALS); ?></pre>
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();
}
2

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

Makes things much nicer and clearer: dBug

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

3
  • 1
    Not a really good idea, really. First, it's too much code compared to a simple "<pre>" 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 <pre>? And if you are going to make it pretty, you really should consider using dbug
    – Sylverdrag
    Apr 10, 2011 at 14:19
  • it's great idea if you want something simple, portable and more pretty then <pre>/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, 2011 at 9:19
  • More simple, portable and pretty than <pre>? Surely you jest. If you need a quick look in the array, nothing is easier than <pre>. 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, 2011 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.

2
  • Not a good idea for debugging purposes. See comments to Gilsilas' answer.
    – Sylverdrag
    Apr 10, 2011 at 14:23
  • @Sylverdrag it's good enough for his current task. In fact, he's getting a mess with multidimensional arrays which he don't need at all. Apr 10, 2011 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.