Skip to content

Instantly share code, notes, and snippets.

@joshdutcher
Last active March 28, 2019 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshdutcher/f44f63c8de63d08c05ed to your computer and use it in GitHub Desktop.
Save joshdutcher/f44f63c8de63d08c05ed to your computer and use it in GitHub Desktop.
pretty function for debugging php arrays, objects, variables. etc
function debug($content, $comments=false) {
$backtrace = debug_backtrace();
$calling_file = $backtrace[0]['file'];
$calling_line = $backtrace[0]['line'];
if ($comments) {
// prints the content inside an html comment, requiring user to view source to read it
$prefix = "<!--" . "\r\n" . $calling_file . " line " . $calling_line . "\r\n";
$suffix = "\r\n" . "-->" . "\r\n";
} else {
$prefix = '<div style="width: 100%; text-align:left;" align="left">' .
'<pre style="align: left; font-size: 0.8em; font-family: \'Courier New\', Courier, monospace; background-color: #111; padding: 5px; color: #CCF; border: 1px solid #999;">' .
'<span style="font-weight: bold; color: #FFA">' . $calling_file . '</span> line <span style="font-weight: bold; color: #FFA">' . $calling_line . '</span>:<br/><br/>';
$suffix = '</pre></div>';
}
echo $prefix;
switch(gettype($content)) {
case 'array':
print_r($content);
break;
case 'object':
var_dump($content);
break;
default:
echo $content;
}
echo $suffix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment