Last active
March 28, 2019 16:12
-
-
Save joshdutcher/f44f63c8de63d08c05ed to your computer and use it in GitHub Desktop.
pretty function for debugging php arrays, objects, variables. etc
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($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