Making PHP var_dump() values display one line per value

前端 未结 14 1228
隐瞒了意图╮
隐瞒了意图╮ 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:22

    var_export will give you a nice output. Examples from the docs:

    $a = array (1, 2, array ("a", "b", "c"));
    echo '<pre>' . var_export($a, true) . '</pre>';
    

    Will output:

    array (
      0 => 1,
      1 => 2,
      2 => 
      array (
        0 => 'a',
        1 => 'b',
        2 => 'c',
      ),
    )
    
    0 讨论(0)
  • 2020-12-23 16:22

    I did a similar solution. I've created a snippet to replace 'vardump' with this:

    foreach ($variable as $key => $reg) {
        echo "<pre>{$key} => '{$reg}'</pre>";
    }
    var_dump($variable);die;
    

    Ps: I'm repeating the data with the last var_dump to get the filename and line

    So this: Became this:

    Let me know if this will help you.

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

    Personally I like the replacement function provided by Symfony's var dumper component

    Install with composer require symfony/var-dumper and just use dump($var)

    It takes care of the rest. I believe there's also a bit of JS injected there to allow you to interact with the output a bit.

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

    Wrap it in <pre> tags to preserve formatting.

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

    I really love var_export(). If you like copy/paste-able code, try:

    echo '<pre>' . var_export($data, true) . '</pre>';
    

    Or even something like this for color syntax highlighting:

    highlight_string("<?php\n\$data =\n" . var_export($data, true) . ";\n?>");
    
    0 讨论(0)
  • 2020-12-23 16:26

    For devs needing something that works in the view source and the CLI, especially useful when debugging unit tests.

    echo vd([['foo'=>1, 'bar'=>2]]);
    
    function vd($in) {
      ob_start(); 
      var_dump($in);
      return "\n" . preg_replace("/=>[\r\n\s]+/", "=> ", ob_get_clean());
    }
    

    Yields:

    array(1) {
      [0] => array(2) {
        'foo' => int(1)
        'bar' => int(2)
      }
    }
    
    0 讨论(0)
提交回复
热议问题