Created
December 31, 2014 15:22
-
-
Save hpohlmeyer/53fc5b6b3017894eff8a to your computer and use it in GitHub Desktop.
My PHP debug function.
This file contains hidden or 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
<?php | |
/** | |
* Debug function | |
* | |
* @param mixed $data The data, that should be debugged. | |
* @param bool $dump (default: false) Show var_dump output. | |
* @param bool $console (default: false) Print debug message to the console. | |
* @return void | |
*/ | |
function debug($data, $dump = false, $console = false) { | |
$line = debug_backtrace()[0]['line']; | |
if ($dump) { | |
ob_start(); | |
var_dump($data); | |
$data = ob_get_clean(); | |
} else { | |
$data = var_export($data, true); | |
} | |
if (!$console) { | |
$msg = '<pre>'; | |
$msg .= '<strong>'.$line.': </strong>'; | |
$msg .= $data; | |
$msg .= '</pre>'; | |
} else { | |
$msg = '<script>console.log('; | |
$msg .= substr_replace(json_encode($data), $line.': ', 1, 0); | |
$msg .= ');</script>'; | |
} | |
echo $msg; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment