只是想知道 -
调试 PHP 时 - 您希望如何输出测试数据以查看发生了什么?我一直注意到我的很多PHP回声测试都搞砸了我的CSS。 有没有人有一种很好的干净方法来查看结果而不会搞砸网站本身?
你应该尝试error_log函数。它会将您的调试输出直接记录到 Web 服务器日志中,而不是在您的页面中。
另一种方法是在注释标记之间回显:
echo '<!-- This is a debug message! -->';
我喜欢
使用:
error_log("message and vars here");
这取决于服务器配置,但如果您可以使用它,您将获得一个漂亮的日志文件。非常有用。
是的,如果你有这种设置,请使用 Apache 错误日志,并使用 tail -f。 使用此处找到的error_log
函数。
我快速调试的首选方法之一是:
echo '<pre>';
print_r($variable);
echo '</pre>';
die;
但是,如果您真的想从应用程序中获取大量优质数据,请查看 http://xdebug.org/
我通常将其用于调试目的。
namespace Debug;
function print_r($var, $return)
{
$s = '<pre>' . htmlspecialchars('print_r($var, true)) . '</pre>';
if ($return) {
return $s;
} else {
echo $s;
}
}
我认为如果你在echo
ing之后渲染页面,它不应该弄乱你的css。你可能想使用以下代码;
echo '<pre> data ';
print_r( $data) ;
echo '</pre>' ;
有一个不错的工具,叫做FirePHP。它集成到FireBug,并使用HTTP标头与控制台进行通信。有关详细信息,请参阅 http://www.firephp.org/
var_dump在
页面末尾和呈现的数据之后?尽管我通常var_dump页面顶部的调试数据,因为我几乎总是使用某种输出缓冲机制。
你可以尝试用HTML <pre>
标签包装东西。
我使用彩色输出,但这"拧紧"了很多网站的布局,但它提供了最多的信息:
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 .= " # count=[<b>" . count($vInput) . "</b>] 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(" "," ",$t);
}
Kou 可以使用var_export
:
echo '<pre class="bottomerrorlog">';
var_export($variable);
echo '</pre>';
并将 CSS 类定义为 CSS 类.bottomerrorlog
最初隐藏的东西。
例如,一个空白区域,只有当您通过 jQuery 将鼠标悬停在其上时才会扩展