Advanced PHP Debug Function

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

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:

//For example, displaying $_POST information

//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>';	

Tags: edmondscommerce