2011-02-26 74 views

回答

2

如果您想轻松打印数组或其他PHP值的内容,请使用var_dump。调用exit()垂直于这一点,我认为这是很清楚的写:

var_dump($arr); 
exit(1); 

另一种方法是登录你的输出,这可能会造成更多的有用的,如果你不想通过你的输出HTML看过筛为var_dump输出:

error_log(var_export($arr)); 
exit(1); 
1

也许你可以做一个调试退出功能进行调试与print_r(打印变量的人类可读的版本)

function dexit($array, $status = 0) { 
    print_r($array); 
    exit($status); 
} 

那么,在你的代码的任何地方,你可以只

dexit($array); 
// or 
dexit($array, 0); 

,但它并不难,只是使用的print_r在线两种方式:)

1

试试这个,你有两个选择,调试或后续代码var_dump

function debug_exit($array, $type = 1) { 
    if ($type == 1) { print_r($array); } 
    if ($type == 2) { var_dump($array); } 
    exit(); 
} 
0

以下是我使用的是,x_r()。跟踪来自this example在PHP文档。跟踪的原因是,您/他人可以找到哪个/什么x_r()被调用通过。该exit()是可选的,如果你需要看到主题下print_r()一些原因:

// Exits with a print_r and call trace for debugging 

if (!function_exists('x_r')) { 
    function x_r($obj, $exit = true, $return = true) { 

     // echo the obj first 
     echo '<pre style="background: #FFFFFF;">', print_r($obj, $return), '</pre>'; 

     // include a debug call trace 
     $e = new Exception(); 
     $trace = explode("\n", $e->getTraceAsString()); 

     // reverse array to make steps line up chronologically 
     $trace = array_reverse($trace); 
     array_shift($trace); // remove {main} 
     array_pop($trace); // remove call to this method 
     $length = count($trace); 
     $result = array(); 

     for ($i = 0; $i < $length; $i++) { 
      $result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering 
     } 

     // echo call trace 
     echo '<pre style="background: #FFFFFF;">', print_r($result, $return), '</pre>'; 

     if ($exit === true) { 
      exit(); 
     } 
    } 
} 

这里是要点:https://gist.github.com/dhaupin/d9d48328dbe312f6c0de