Skip to content

Instantly share code, notes, and snippets.

@aaroncampbell
Last active December 10, 2015 19:48
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 aaroncampbell/4483989 to your computer and use it in GitHub Desktop.
Save aaroncampbell/4483989 to your computer and use it in GitHub Desktop.
Dump function for convenient debugging. It can handle anything var_dump() can, but also takes a title that it will insert above the data, as well as formatting the data for display in HTML, PLAINTEXT, or as an HTML comment.
<?php
/**
* For use with debugging
*/
if ( !function_exists('dump') ) {
/**
* To be used like a more flexible more developer friendly var_dump.
* Basically you can pass a variable to dump as well as a title and choose
* whether it returns that data to you to echo out or Use this as a var_dump
*
* @param mixed $v Variable to dump
* @param string $title Title to display above dump
* @param string $format One of 'html' (default), 'plaintext', or 'htmlcomment'
* @param bool $return true to return the content, false to echo (default)
*
* @return null|string If $return is true, returns all output as string
*/
function dump($v, $title = '', $format = 'html', $return = false ) {
$format = strtolower( $format );
if ( ! in_array( $format, array( 'html', 'plaintext', 'htmlcomment' ) ) )
$format = 'html';
if ( 'html' == $format ) {
$before_title = '<h4>';
$after_title = '</h4>';
$before_content = '<pre>';
$after_content = '</pre>';
} else {
$after_title = $before_title = '::';
$after_content = $before_content = "\r\n";
}
if ( ! empty( $title ) )
$title = $before_title . htmlentities($title) . $after_title;
ob_start();
var_dump( $v );
$v = ob_get_clean();
if ( 'html' == $format )
$v = esc_html( $v );
$v = $title . $before_content . $v . $after_content;
if ( 'htmlcomment' == $format )
$v = "<!--\r\n{$v}\r\n-->";
if ( $return )
return $v;
echo $v;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment