开发者

How to make the output of server headers more readable?

开发者 https://www.devze.com 2023-02-23 00:01 出处:网络
Given the following php5 code that output a gigantuous amount of diffi开发者_高级运维cult to read code:

Given the following php5 code that output a gigantuous amount of diffi开发者_高级运维cult 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() :

  • colors
  • formating


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

     

How to make the output of server headers more readable?


(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


  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消