7

Just wondering -

When debuging PHP - how do you like to output the test data to see whats going on? I've been noticing that a lot of my PHP echo testing is screwing with my CSS. Does anyone have a good clean method of seeing the results without screwing with the site itself?

11 Answers 11

9

You should try error_log function. It will log your debug output directly into the web server logs, and not in your page.

Another way is to echo between comments markups:

echo '<!-- This is a debug message! -->';
Sign up to request clarification or add additional context in comments.

Comments

5

I like to use:

error_log("message and vars here");

It depends on the server configuration, but if you can use it, you get a nice log-file. Very useful.

Comments

4

One of my go-to methods for a quick debug is this:

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

However, if you're really looking into getting a lot of good data from your application, check out http://xdebug.org/

2 Comments

Lol i made the exact same function calling it prePrint :)
Ideally, you would escape the result using htmlspecialchars too.
4

Yes, use the Apache error log, if you have that kind of setup, with tail -f. Use the error_log function found here.

1 Comment

interesting, i hadn't heard of that. Very useful.
2

I typically use this for my debugging purposes.

namespace Debug;

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

2 Comments

Does this override the default print_r function?
No, you'd have to do \Debug\print_r(...) or import it
1

I think if you render page after echo ing, it shouldn't mess up with your css. You may want to use followint code ;

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

Comments

1

There is a nice tool, called FirePHP. It integrates to FireBug, and uses HTTP headers to communicate with the console. For more, see http://www.firephp.org/

Comments

0

var_dump at the end of the page and after the rendered data? Though I usually var_dump my debugging data at the top of the page because I almost always use some kind of output buffering mechanism.

Comments

0

You could try wrapping things in HTML <pre> tags.

Comments

0

I use a colored output but that "screws" with the layout of the site a lot, but it is most informative:

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);

}

2 Comments

I think adding a screenshot of a sample output to this answer will be cool
feel free to add one
0

Kou can use var_export:

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

and define the css class .bottomerrorlog to something hidden to the eye at first.

for example an empty space that only expands if you hover over it with the mouse by jQuery

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.