Last active
September 23, 2015 21:57
-
-
Save amgraham/621681 to your computer and use it in GitHub Desktop.
Lets you know what is going on with a variable in php.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function debug(&$var, $r = NULL) { | |
// if $var is an empty string, we're being called recursively | |
// this is an extreme guess, and might cause problems | |
// now we determine the variable name | |
$old = $var; | |
// var = $new = "unique".rand()."value"; | |
$new = $var; | |
$var = "unique".rand()."value"; | |
$new = $var; | |
$vname = FALSE; | |
foreach($GLOBALS as $key => $val) { | |
if($val === $new) $vname = $key; | |
} | |
$var = $old; | |
$n = $vname; | |
if ($r == null) { | |
echo "<div style='text-align: left;margin-top: 1em; margin-bottom: 1em; font-size: 110%; border: 1px solid #ccc; background: #efefef; padding: .5em; font-family: monospace;'>"; | |
echo "<div style='padding: .2em; margin-bottom: .2em; border: 1px solid #ccc; background: #fff;'>"; | |
echo "<span style='padding-right: .2em; float: right; color: red;'>"; | |
echo gettype($var); | |
echo "</span>"; | |
echo "<span style='color: green;'>\$" . $n . "</span></div>"; | |
} else { | |
echo "<div style='margin-left: .5em;'>"; | |
} | |
// if $var is an array, we need to give it some special stuff | |
if (is_array($var)) { | |
echo "<div style='border-bottom: 1px dotted #ccc; padding-bottom: .5em; margin-bottom: .5em;'>"; | |
// for each $key=>$value pair | |
foreach ($var as $k=>$v) { | |
// we set $c (count) here as an empty string so it won't do anything, if we change it, we need to alter the display/layout | |
$c = ""; | |
if (count($v) != 1) { | |
$c = count($v) . " items"; | |
} | |
// if $c is empty the results will be displayed next to the $k, | |
// otherwise we're display the number of items, then on a new line, go over them | |
echo "<span style='font-weight: bold;'>$k</span> → $c"; | |
if (is_array($v)) { | |
debug($v, true); | |
} else { | |
echo $v . "<br />"; | |
} | |
} | |
echo "</div>"; | |
} else { | |
echo $var; | |
} | |
echo "</div>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment