Skip to content

Instantly share code, notes, and snippets.

@hpohlmeyer
Created December 31, 2014 15:22
Show Gist options
  • Save hpohlmeyer/53fc5b6b3017894eff8a to your computer and use it in GitHub Desktop.
Save hpohlmeyer/53fc5b6b3017894eff8a to your computer and use it in GitHub Desktop.
My PHP debug function.
<?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