Skip to content

Instantly share code, notes, and snippets.

@cassler cassler/printp.php

Last active Aug 29, 2015
Embed
What would you like to do?
Rich Print R
function print_p($value = false, $exit = false, $return=false, $recurse=false) {
if ($return === true && $exit === true)
$return = false;
$tab = str_repeat(" ", 8);
if ($recurse == false) {
$recurse = 0;
$output = '<a class="btn btn-primary" type="button" data-toggle="collapse" data-target="#debugInfo" aria-expanded="false">
Debug Info
</a><pre id="debugInfo" class="collapse" style="width:100%; height:1px; border: 1px dotted #ccc; background-color: #eee; display: block; padding: 20px;">';
$backtrace = debug_backtrace();
$output .= '<b>Line: </b>'.$backtrace[0]['line'].'<br>';
$output .= '<b>File: </b> '.$backtrace[0]['file'].'<br>';
$indent = "";
} else {
$output = '';
$indent = str_repeat("&nbsp;", $recurse * 8);
}
if (is_array($value)) {
if ($recurse == false) {
$output .= '<b>Type: </b> Array<br>';
$output .= "<br>array (<br>";
} else {
$output .= "array (<br>";
}
$items = array();
foreach ($value as $k=>$v) {
if (is_object($v) || is_array($v))
$items[] = $indent.$tab."'".$k."'=>".print_p($v, false, true, ($recurse+1));
else
$items[] = $indent.$tab."'".$k."'=>".($v===null ? "NULL" : "'".$v."'");
}
$output .= implode(',<br>', $items);
if ($recurse == false)
$output .= '<br>)';
else
$output .= '<br>'.$indent.')';
} elseif (is_object($value)) {
if ($recurse == false) {
$output .= '<b>Type: </b> Object<br>';
$output .= '<br>object ('.get_class($value).'){'."<br>";
} else {
$output .= "object (".get_class($value)."){<br>";
}
// needed conditional because base class function dump is protected
$vars = get_object_vars($value);
$vars = (is_array($vars) == true ? $vars : array());
$items = array();
foreach ($vars as $k=>$v) {
if (is_object($v) || is_array($v))
$items[] = $indent.$tab."'".$k."'=>".print_p($v, false, true, ($recurse+1));
else
$items[] = $indent.$tab."'".$k."'=>".($v===null ? "NULL" : "'".$v."'");
}
$output .= implode(',<br>', $items);
$vars = get_class_methods($value);
$items = array();
foreach ($vars as $v) {
$items[] = $indent.$tab.$tab.$v;
}
$output .= '<br>'.$indent.$tab.'<b>Methods</b><br>'.implode(',<br>', $items);
if ($recurse == false)
$output .= '<br>}';
else
$output .= '<br>'.$indent.'}';
} else {
if ($recurse == false) {
$output .= '<b>Type: </b> '.gettype($value).'<br>';
$output .= '<b>Value: </b> '.$value;
} else {
$output .= '('.gettype($value).') '.$value;
}
}
if ($recurse == false)
$output .= '</pre>';
if ($return === false)
echo $output;
if ($exit === true)
die();
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.