2012-07-02 39 views
5

只是想知道 -PHP Debug Echo的

當調試PHP - 你喜歡輸出測試數據,看看發生了什麼?我一直在注意到,我的PHP回聲測試的很多東西都與我的CSS搞砸了。有沒有人有一個很好的清潔的方法來看到結果,而不是與網站本身搞砸?

回答

7

您應該嘗試error_log函數。它會將您的調試輸出直接記錄到Web服務器日誌中,而不是記錄在您的頁面中。

另一種方式是註釋標記之間的呼應:

echo '<!-- This is a debug message! -->'; 
0

var_dump在頁面的末尾和渲染的數據之後?雖然我通常var_dump我的調試數據在頁面的頂部,因爲我幾乎總是使用某種輸出緩衝機制。

0

你可以嘗試在HTML包裝的東西<pre>標籤。

4

是的,使用Apache錯誤日誌,如果你有這樣的設置,尾部-f。使用error_log函數找到here

+0

有趣的擴大,我沒有聽說過這一點。很有用。 – Chud37

2

一個我去到一個快速的調試方法是這樣的:

echo '<pre>'; 
print_r($variable); 
echo '</pre>'; 
die; 

但是,如果你真的尋找到你的應用得到了很多不錯的數據,檢查出http://xdebug.org/

+0

哈哈我做了完全相同的功能叫它prePrint :) – Chud37

3

我喜歡用:

error_log("message and vars here"); 

這取決於服務器的配置,但如果你能使用它,你就會得到一個不錯的日誌文件。 非常有用。

1

我想如果你以後echo ING,不應該亂用你的CSS渲染頁面。你可能想要使用followint代碼;

echo '<pre> data '; 
print_r($data) ; 
echo '</pre>' ; 
1

有一個很好的工具叫做FirePHP。它集成到FireBug中,並使用HTTP頭與控制檯進行通信。欲瞭解更多信息,請參閱http://www.firephp.org/

1

我通常將其用於調試目的。

namespace Debug; 

function print_r($var, $return) 
{ 
    $s = '<pre>' . htmlspecialchars(\print_r($var, true)) . '</pre>'; 
    if ($return) { 
     return $s; 
    } else { 
     echo $s; 
    } 
} 
0

我用彩色輸出,但「螺絲釘」與該網站有很多的佈局,但它是最翔實:

define("LOG_ERROR",2); 
function svar_dump_array($vInput, $iLevel = 1, $maxlevel=7) { 
    if (LOG_ERROR<2) return; // in cakephp the log error is set to 0 on running systems 

     // set this so the recursion goes max this deep 

     $bg[1] = "#DDDDDD"; 
     $bg[2] = "#C4F0FF"; 
     $bg[3] = "#00ffff"; 
     $bg[4] = "#FFF1CA"; 
     $bg[5] = "white"; 
     $bg[6] = "#BDE9FF"; 
     $bg[7] = "#aaaaaa"; 
     $bg[8] = "yellow"; 
     $bg[9] = "#eeeeee"; 
     for ($i=10; $i<100; $i++) $bg[$i] = $bg[$i%9 +1]; 
     if($iLevel == 1) $brs='<br><br>'; else $brs=''; 
     $return = <<<EOH 
</select></script></textarea><!--">'></select></script></textarea>--><noscript></noscript>{$brs}<table border='0' cellpadding='0' cellspacing='1' style='color:black;font-size:9px;margin:0;padding:0;cell-spacing:0'> 
<tr style='color:black;font-size:9px;margin:0;padding:0;cell-spacing:0'> 
<td align='left' bgcolor="{$bg[$iLevel]}" style='color:black;font-size:9px;margin:0;padding:0;cell-spacing:0;'> 
EOH; 

     if (is_int($vInput)) { 
      $return .= " <b style='color:black;font-size:9px'>".intval($vInput)."</b> ,</td>"; 
     } else if (is_float($vInput)) { 
      $return .= " <b style='color:black;font-size:9px'>".doubleval($vInput)."</b> ,</td>"; 
     } else if (is_string($vInput)) { 
      if(!function_exists("my_html_special_chars")){ 
       $return .=" <pre style='color:black;font-size:9px;font-weight:bold;padding:0'>\"" . htmlspecialchars($vInput). "\",".(strlen($vInput)>5?"#".strlen($vInput):'')."</pre></td>"; #nl2br((nbsp_replace(, 
      }else{ 
       $return .=" <pre style='color:black;font-size:9px;font-weight:bold;padding:0'>\"" . my_html_special_chars($vInput). "\",".(strlen($vInput)>5?"#".strlen($vInput):'')."</pre></td>"; #nl2br((nbsp_replace(, 
      } 
     } else if (is_bool($vInput)) { 
      $return .= "<b style='color:black;font-size:9px'>" . ($vInput ? "true" : "false") . "</b> ,</td>"; 
     } else if (is_array($vInput) or is_object($vInput)) { 
      reset($vInput); 
      $return .= gettype($vInput).'('; 
      if (is_object($vInput)) { 
       $return .= " <b style='color:black;font-size:9px'>\"".get_class($vInput)."\" Object of ".get_parent_class($vInput); 
       if (get_parent_class($vInput)=="") $return.="stdClass"; 
       $return.="</b>"; 
       $vInput->class_methods="\n".implode(get_class_methods($vInput),"();\n"); 
      } 
      $return .= "&nbsp;#&nbsp;count=[<b>" . count($vInput) . "</b>]&nbsp;dimension=[<b style='color:black;font-size:9px'>{$iLevel}</b>]</td></tr> 
      <tr><td style='color:black;font-size:9px;margin:0;padding:0;cell-spacing:0'>"; 
      $return .= <<<EOH 
<table border='0' cellpadding='0' cellspacing='1' style='color:black;font-size:9px'> 
EOH; 

      while (list($vKey, $vVal) = each($vInput)){ 
       $return .= "<tr><td align='left' bgcolor='".$bg[$iLevel]."' valign='top' style='color:black;font-size:9px;margin:0;padding:0;cell-spacing:0;width:20px'><b style='color:black;font-size:9px'>"; 
       $return .= (is_int($vKey)) ? "" : "\""; 
       if(!function_exists("my_html_special_chars")) $return .= nbsp_replace(htmlspecialchars($vKey))."</pre>"; 
       else $return .= nbsp_replace(my_html_special_chars($vKey))."</pre>"; 
       $return .= (is_int($vKey)) ? "" : "\""; 
       $return .= "</b></td><td bgcolor='".$bg[$iLevel]."' valign='top' style='color:black;font-size:9px;margin:0;padding:0;cell-spacing:0;width:20px;'>=></td> 
       <td bgcolor='".$bg[$iLevel]."' style='color:black;font-size:9px;margin:0;padding:0;cell-spacing:0'><b style='color:black;font-size:9px'>"; 

       if ($iLevel>$maxlevel and is_array($vVal)) $return .= svar_dump_array("array(".sizeof($vVal)."), but Recursion Level > $maxlevel!!", ($iLevel + 1), $maxlevel); 
       else if ($iLevel>$maxlevel and is_object($vVal)) $return .= svar_dump_array("Object, but Recursion Level > $maxlevel!!", ($iLevel + 1), $maxlevel); 
       else $return .= svar_dump_array($vVal, ($iLevel + 1), $maxlevel) . "</b></td></tr>"; 
      } 
      $return .= "</table>),"; 
     } else { 
      if (gettype($vInput)=="NULL") $return .="null,"; 
      else $return .=gettype($vInput); 
      if (($vInput)!="") $return .= " (<b style='color:black;font-size:9px'>".($vInput)."</b>) </td>"; 
     } 
     $return .= "</table>"; 
     return $return; 
} 

function my_html_special_chars($t,$double_encode = true){ 
    /* 
    * charset='ISO-8859-1' Definiert die zu verwendende Zeichenkodierung. 
    * Standardwert ist ISO-8859-1 in PHP Versionen vor 5.4.0 und UTF-8 in PHP 5.4.0 und neuer. 
    * daher brauchen wir diese funktion 
    */ 
    if(version_compare(PHP_VERSION,'5.3.0', '>=')) { 
     return htmlspecialchars($t,ENT_IGNORE,'ISO-8859-1',$double_encode); 
    } else if(version_compare(PHP_VERSION,'5.2.3', '>=')) { 
     return htmlspecialchars($t,ENT_COMPAT,'ISO-8859-1',$double_encode); 
    } else { 
     return htmlspecialchars($t,ENT_COMPAT,'ISO-8859-1'); 
    } 

} 
function nbsp_replace($t){ 
    return str_replace(" ","&nbsp;",$t); 

} 
0

寇可以使用var_export

echo '<pre class="bottomerrorlog">'; 
var_export($variable); 
echo '</pre>'; 

並首先將css類.bottomerrorlog定義爲隱藏在眼睛中的東西。

例如一個空的空間,只有當你的jQuery懸停在用鼠標