I always include debugging code in all may major projects. I use an object (e.g. $CFG) to store the settings the control the code's behavior, which includes a $CFG->debug variable to control the level of debugging verbosity. I set it to 0 before rollout, which ensures that even if some debugging code was left in accidentally, this will be deactivated and not spew debugging information in a live website.
I use the following functions to facilitate debugging messages. They live in a single code module which I only need include() to use it. Note the global reference to $CFG:
<?php
// Dump the contents of a variable into HTML comments for debugging:
function debugVar ($var) {
global $CFG;
if ($CFG->debug) {
echo "\n<!--\nDEBUG INFO: \n";
if (is_array ($var))
print_r ($var)
else
var_dump ($var);
echo "\n-->\n";
}
}
// Dump function parameters into HTML comments for debugging:
function debugFunc () {
global $CFG;
if ($CFG->debug) {
$argv = func_get_args ();
echo "\n<!--\nDEBUG INFO: \n";
print_r ($argv);
echo "\n-->\n";
}
}
// Dump a debugging message into HTML comments for debugging:
function debugMsg ($msg) {
global $CFG;
if ($CFG->debug) {
echo "\n<!--\nDEBUG INFO: \n";
echo $msg;
echo "\n-->\n";
}
}
?>
Usage is simple:
To check a variable, call <?php debugVar ($variable); ?>
To check parameters passed to a function, call
<?php debugFunc ($var1, $var2, $var3 ...); ?>
To print a debug message, call
<?php debugMsg ("Function xyz returned " . $retVal); ?>
Then check your HTML source to find the debugging output in the comments.
Don't forget to set $CFG->debug to 0 before rollout. If you're paranoid you can also include a javascript alert() box to be triggered by a : $CFG->debug > 0" condition upon each page reload, although that gets really annoying really fast. :-)
// Frank
I usually use this simple function in combo with a die(); in order to have on screen the value of a variable or array:
<?php
function debug_view ( $what ) {
echo '<pre>';
if ( is_array( $what ) ) {
print_r ( $what );
} else {
var_dump ( $what );
}
echo '</pre>';
}
?>
Here a best printed var_dump : show_php
It's a reccursive function for output many kind of structured data (array, class, etc ...)
<?php
function show_php($var,$indent=' ',$niv='0')
{
$str='';
if(is_array($var)) {
$str.= "<b>[array][".count($var)."]</b><br />";
foreach($var as $k=>$v) {
for($i=0;$i<$niv;$i++) $str.= $indent;
$str.= "$indent<em>\"{$k}\"=></em>";
$str.=show_php($v,$indent,$niv+1);
}
}
else if(is_object($var)) {
$str.= "<b>[objet]-class=[".get_class($var)."]-method=[";
$arr = get_class_methods($var);
foreach ($arr as $method) {
$str .= "[function $method()]";
}
$str.="]-";
$str.="</b>";
$str.=show_php(get_object_vars($var),$indent,$niv+1);
}
else {
$str.= "<em>[".gettype($var)."]</em>=[{$var}]<br />";
}
return($str);
}
?>
EXAMPLE :
array example to debug :
<?php
$tab=array(
"first"=>"firstValue",
"second"=>array(
"first"=>1,
"second"=>2,
"third"=>array(
"first"=>"one",
"second"=>"two"
),
),
);
?>
result of show_php :
<?php
echo "tab=".show_php($tab);
?>
tab=[array][2]
"first"=>[string]=[firstValue]
"second"=>[array][3]
"first"=>[integer]=[1]
"second"=>[integer]=[2]
"third"=>[array][2]
"first"=>[string]=[one]
"second"=>[string]=[two]
result of var_dump :
<?php
echo "tab=";
var_dump($tab);
echo "<br>";
?>
tab=array(2) { ["first"]=> string(10) "firstValue" ["second"]=> array(3) { ["first"]=> int(1) ["second"]=> int(2) ["third"]=> array(2) { ["first"]=> string(3) "one" ["second"]=> string(3) "two" } } }
If you don't find a syntax error, you can comment out a block where you assume the error (or put it out of the document by [ctrl] + [X], but keep a copy on your HD for the case, your computer crashes) and check, if the syntax error is still there.
If not, it must be anywhere in your commented text; if yes, it must be somewhere else.
If you want to locate the error better, do it again with an other and/or smaller piece of code, till you get it.
I still find that printing out variable values at problem points in the code is one of the easiest ways for me to debug. If you're interested in knowing the full contents of an object/array/scalar, then use
var_dump($var).