Skip to content

Instantly share code, notes, and snippets.

@timstl
Last active August 29, 2015 14:06
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 timstl/19862794f1661e47e9fa to your computer and use it in GitHub Desktop.
Save timstl/19862794f1661e47e9fa to your computer and use it in GitHub Desktop.
Debug_Display debugging to screen function
<?php
/*
Based on debugging function found in CMS Made Simple http://www.cmsmadesimple.org/
Meant for use in WordPress (ability to only output to 'manage_options' users)
Pass in 'false' as section param to show to everyone.
*/
if (!function_exists('debug_display'))
{
function debug_display($var, $admins=true, $title="", $echo_to_screen = true, $use_html = true)
{
if ($admins == true && !current_user_can('manage_options')) { return; }
$titleText = "Debug: ";
if($title)
{
$titleText = "Debug display of '$title':";
}
ob_start();
if ($use_html)
{
echo "<p><b>$titleText</b><pre>\n";
}
if(is_array($var))
{
echo "Number of elements: " . count($var) . "\n";
print_r($var);
}
elseif(is_object($var))
{
print_r($var);
}
elseif(is_string($var))
{
print_r(htmlentities(str_replace("\t", ' ', $var)));
}
elseif(is_bool($var))
{
echo $var === true ? 'true' : 'false';
}
else
{
print_r($var);
}
if ($use_html)
{
echo "</pre></p>\n";
}
$output = ob_get_contents();
ob_end_clean();
if($echo_to_screen)
{
echo $output;
}
return $output;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment