Advanced PHP Debug Function
Mar 20, 2008 · 2 minute readCategory: php
This is post is now quite old and the the information it contains may be out of date or innacurate.
If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub
Heres How to Use It:
//For example, displaying $_POST information
dbug($_POST);
//If using inside a function
function something($blah){
dbug($blah, get_defined_vars());
}
Note that if called within the scope of a function (for example) you must give get_defined_vars().
And here are the neccessary functions
//DEbug Functions
//convert tabs to spaces
function tab2space($text){
$text = str_replace(' ', ' ', $text);
$text = str_replace("\t", ' ', $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>';
}