Created
August 2, 2018 14:50
-
-
Save vctrtvfrrr/d29403d4b6f9b8c174bfa5f44cf53450 to your computer and use it in GitHub Desktop.
PHP Dump and Die
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 helper function. This is a wrapper for var_dump() that adds | |
* the <pre /> tags, cleans up newlines and indents before output. | |
* | |
* @param mixed $var The variable to dump. | |
* @param string $label OPTIONAL Label to prepend to output. | |
* @param bool $die OPTIONAL Die script execution. | |
* @return string | |
*/ | |
if (! function_exists('dd')) { | |
function dd($var, $label = null, $die = true) | |
{ | |
// format the label | |
$label = ($label===null) ? '' : rtrim($label) . ' '; | |
// var_dump the variable into a buffer and keep the output | |
ob_start(); | |
var_dump($var); | |
$output = ob_get_clean(); | |
// neaten the newlines and indents | |
$output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output); | |
if (PHP_SAPI == 'cli') { | |
$output = PHP_EOL . $label | |
. PHP_EOL . $output | |
. PHP_EOL; | |
} else { | |
$output = '<pre>' | |
. $label | |
. $output | |
. '</pre>'; | |
} | |
echo $output; | |
if ($die) { | |
exit; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment