8/22/2005
Debug Variables in PHP
Drop this function into a PHP include and use it to print out variable debugging info during runtime.
function dv($var, $var_name=”, $remote_ip=”) {
if($remote_ip != ” && $remote_ip != $_SERVER[‘REMOTE_ADDR’]) return;
$tab = strlen($var_name) > 6 ? “\t“:“\t\t“;
$var_name = $var_name != ” ? ‘$’ . $var_name . ‘ ‘ . $tab . ‘ = ‘:”;
print(‘<pre> ‘ . $var_name);
var_dump($var);
print(“</pre>\n“);
}
?>
To call, use one of the following:
dv($the_variable, ‘the_variable_name’, ‘192.168.0.1′);
?>
Where:
- $the_variable - the variable to print
- the_variable_name - the name of variable to print
- 192.168.0.1 - your local IP address. Variable will only be echoed when the page is called from this IP address. Useful for debugging on live sites.
The last two parameters are not required but can be used for additional flexibility.
If printing out the same variable at multiple locations in the same page, use successive names for the_variable_name (the_variable_name1, the_variable_name2, etc) to differentiate.
If debugging which file is being processing do:
dv(__FILE__, ‘file’, ‘192.168.0.1′);
?>
This will echo the filename the function is called from.
If debugging which line the script stops processing on do:
dv(__LINE__, ‘line’, ‘192.168.0.1′);
?>
This will echo the line number the function is called from.
Popularity: 15%



June 29th, 2007 at 6:47 am
[…] have written several times about debugging a live site and posted snippets for working on the themes of a live Wordpress install. One trick I haven’t […]