Making PHP var_dump() values display one line per value

前端 未结 14 1217
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 15:41

When I echo var_dump($_variable), I get one long, wrapping line with all varable\'s and values like

[\"kt_login_user\"]=>  string(8) \"teacher1\" [\"kt_lo         


        
相关标签:
14条回答
  • 2020-12-23 16:29

    I didn't wanna stop using var_dump($variable);die(); and using pre tags and loops seems overkill to me, so since I am looking at the dump in a browser, I just right click the page and choose Inspect (I use Chrome). The Elements section of the Developer Tools show the variable in a very readable format.

    0 讨论(0)
  • 2020-12-23 16:35

    You can press Ctrl+U to view the source code. Most Browsers will prettify the output there.

    var_dump is the ugliest way to debug.

    0 讨论(0)
  • 2020-12-23 16:37

    If you got XDebug installed, you can use it's var_dump replacement. Quoting:

    Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes different colors for different types and places limits on the amount of array elements/object properties, maximum depth and string lengths. There are a few other functions dealing with variable display as well.

    You will likely want to tweak a few of the following settings:

    There is a number of settings that control the output of Xdebug's modified var_dump() function: xdebug.var_display_max_children, xdebug.var_display_max_data and xdebug.var_display_max_depth. The effect of these three settings is best shown with an example. The script below is run four time, each time with different settings. You can use the tabs to see the difference.

    But keep in mind that XDebug will significantly slow down your code, even when it's just loaded. It's not advisable to run in on production servers. But hey, you are not var_dumping on production servers anyway, are you?

    0 讨论(0)
  • 2020-12-23 16:38

    Use output buffers: http://php.net/manual/de/function.ob-start.php

    <?php
        ob_start();
        var_dump($_SERVER) ;
        $dump = ob_get_contents();
        ob_end_clean();
    
        echo "<pre> $dump </pre>";
    ?>
    

    Yet another option would be to use Output buffering and convert all the newlines in the dump to <br> elements, e.g.

    ob_start();
    var_dump($_SERVER) ;
    echo nl2br(ob_get_clean());
    
    0 讨论(0)
  • 2020-12-23 16:41

    Yes, try wrapping it with <pre>, e.g.:

    echo '<pre>' , var_dump($variable) , '</pre>';
    
    0 讨论(0)
  • 2020-12-23 16:42

    I've also researched this issue and not found the right answer. This doesn't work for me:

    echo '<pre>' . var_dump($variable) . '</pre>';
    

    This will not provide a nice display of the array for me, with line breaks (I'm using Firefox 31.3.0)

    However, after some experimentation, this solved the problem (notice the php is closed at first):

    ... ?> <pre><?php echo var_dump($variable) ?></pre> <?php ...
    

    This solves the problem and displays a nice, easy-to-read array for me on my browser. You see how the tags are not wrapped in PHP; only the echo var_dump part is.

    0 讨论(0)
提交回复
热议问题