Advanced PHP Debug Function
March 20th, 2008Read More php
I have taken the debug function about as far as I can think of. This function will now take a variable and display it all in a nice easy to read format. It will also give the name of the variable being examined. It works with html, using htmlentites. It preserves the tab layout formatting for arrays making it easy to understand:
Heres How to Use It:
PHP:
-
//For example, displaying $_POST information
-
dbug($_POST);
-
-
//If using inside a function
-
function something($blah){
-
}
Note that if called within the scope of a function (for example) you must give get_defined_vars().
And here are the neccessary functions
PHP:
-
//DEbug Functions
-
-
//convert tabs to spaces
-
function tab2space($text){
-
return $text;
-
}
-
-
//return name of variable passed by reference
-
function vname(&$var, $scope=false){
-
if($scope) $vals = $scope;
-
else $vals = $GLOBALS;
-
$old = $var;
-
$new = 'THISONE';
-
$var = $new;
-
$vname = false;
-
foreach($vals as $key => $val) {
-
if($val !=='var'){
-
if($val === $new) $vname = $key;
-
}
-
}
-
$var = $old;
-
return $vname;
-
}
-
-
-
function dbug(&$item, $scope=false){
-
$vname = vname(&$item,$scope);
-
echo '<hr><h3>Debug Info: $' . $vname . '</h3>' . tab2space(nl2br(htmlentities(var_export($item, true)))) . '<hr>';
-
}











































