代码之家  ›  专栏  ›  技术社区  ›  SeanDowney

您使用哪种php变量调试函数?var_dump、print_r、var_export、其他?[关闭]

  •  4
  • SeanDowney  · 技术社区  · 18 年前

    我个人使用 var_dump ,但很多人喜欢 print_r .

    大家都用什么?利与弊?

    有人有自己的特殊自制功能吗?

    9 回复  |  直到 14 年前
        1
  •  6
  •   A J    10 年前

    我用 print_r() 因为我喜欢漂亮的数组结构。..但是 var_dump 确实会给你更多的信息(比如类型)

    $obj = (object) array(1, 2, 3);
    
    // output of var_dump:
    object(stdClass)#1 (3) {
      [0]=>
      int(1)
      [1]=>
      int(2)
      [2]=>
      int(3)
    }
    
    // output of print_r
    stdClass Object
    (
      [0] => 1
      [1] => 2
      [2] => 3
    )    
    
        2
  •  4
  •   Shoan    18 年前

    我总是使用Xdebug扩展var_dump。它给出了很多冗长的输出。

    请参阅: http://xdebug.org/docs/display 了解更多详情。

        3
  •  4
  •   Noah Goodrich    18 年前

    我使用这些自定义函数取决于我处理的是数组还是单个值:

    function show($array)
    {
        echo '<pre>';
        print_r($array);
        echo '</pre>';
    }
    
    function prn($var)
    {
        echo '<br/>' . $var . '<br/>';
    }
    

    我发现这些函数简化了故障排除,因为我通常需要格式化输出,以便我可以在屏幕上轻松地浏览它。

    对于更复杂的故障排除,我们使用Exception类的扩展版本,它将通过电子邮件发送堆栈跟踪以及特定的错误消息。这给了我所涉及的函数、所涉及的文件、错误中涉及的行以及我创建的任何自定义消息,以便我确切地知道发生了什么。为了增加一层故障排除,我们还将这些错误记录在正在访问的数据库中。

        4
  •  2
  •   PhiLho    18 年前

    我只使用print_r,以及几个包装器函数来存储我在代码中放入的各种DebugPrint,还有一个在页脚上用于将堆栈转储到页面(或文件)中。

    我现在也尝试使用XDebug。D

    好的,为了记录在案,我给出了我的小功能。..

    // Primitive debug message storage
    // $level = "Info", "Warn", "Error", "Title"
    function DebugPrint($toDump, $level = "Info") {
      global $debugMode, $debugDump, $debugCount;
    
      if ($debugMode != 'N') {
        $debugDump[$debugCount++] = "<div class='Dbg$level'>" . $toDump . "</div>\n";
      }
    }
    
    // Initialize debug information collection
    $debugMode = 'N'; // N=no, desactivated, P=dump to Web page, F=dump to file
    $debugSavePath = 'C:\www\App\log_debug.txt'; // If mode F
    $debugDump = array();
    $debugCount = 0;
    
    // Primitive debug message dump
    function DebugDump() {
      global $debugMode, $debugSavePath, $debugDump, $debugCount;
    
      if ($debugMode == 'F') {
        $fp = fopen($debugSavePath, "a"); #open for writing
      }
      if ($debugCount > 0) {
        switch ($debugMode) {
        case 'P':
            echo '<div style="color: red; background: #8FC; font-size: 24px;">Debug:<br />
    ';
            for ($i = 0; $i < $debugCount; $i++) {
                echo $debugDump[$i];
            }
            echo '</div>
    ';
            break;
        case 'F':
            for ($i = 0; $i < $debugCount; $i++) {
                fputs($fp, $debugDump[$i]);
            }
            break;
    //~         default:
    //~             echo "debugMode = $debugMode<br />\n";
        }
      }
      if ($fp != null) {
        fputs($fp, "-----\n");
        fclose($fp);
      }
    }
    
    // Pre array dump
    function DebugArrayPrint($array) {
    global $debugMode;
    
      if ($debugMode != 'N') {
        return "<pre class='ArrayPrint'>" . print_r($array, true) . "</pre>";
      } else return "";    // Gain some microseconds...
    }
    

    感兴趣的是将输出延迟到页面末尾,避免混淆实际页面。

        5
  •  1
  •   Ken    18 年前

    如果你想避免向浏览器发送错误,但想利用var_dump和print_r的优势,那么看看输出缓冲:

    ob_start();
    var_dump($this); echo $that; print_r($stuff);
    $out = ob_get_contents();
    ob_end_clean();
    
    user_error($out);
    

    一些好的阅读 http://www.php.net/manual/en/book.outcontrol.php

        6
  •  1
  •   Meisner    18 年前

    我发现标准PHP实现的变量转储函数缺少一些非常有用的东西,即只打印表达式的一部分的能力。

    这里有一个函数可以解决这个问题:

    function dump($val) {
        echo '<pre>'.var_export($val,true).'</pre>';
        return $val;
    }
    

    现在,我可以围绕我需要知道的表达式进行函数调用,而不会中断通常的代码执行流程,如下所示:

    $a=2+dump(2*2);
    

    使用鲜为人知的 var_export 我还消除了实现输出缓冲以对结果进行后处理的需要。

        7
  •  1
  •   user22960    18 年前

    当您生成二进制答案(即使用GD库的图像)时,您可以使用有效的调优标头:

    header('X-eleg:'.serialize($yourstuff));

    并使用Firefox的Http标头扩展来“监视”它。

        8
  •  0
  •   Peter Bailey    18 年前

    通常使用print_r(),但var_dump()为基元提供了更好的信息。

    话虽如此,我大部分时间都在做 实际的 使用Zend服务器调试器进行调试。

        9
  •  0
  •   rg88    17 年前

    var_dump。安装XDebug后,它可以很好地格式化和着色输出(无需 <pre> 周围的标签)。

    推荐文章