Here my little contribution for a simple yet handy debug function :
<?php
function dbug() {
static $output = '', $doc_root;
$args = func_get_args();
if (!empty($args) && $args[0] === 'print') {
$_output = $output;
$output = '';
return $_output;
}
if (!isset($doc_root)) {
$doc_root = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);
}
$backtrace = debug_backtrace();
$line = htmlspecialchars($backtrace[0]['line']);
$file = htmlspecialchars(str_replace(array('\\', $doc_root), array('/', ''), $backtrace[0]['file']));
$class = !empty($backtrace[1]['class']) ? htmlspecialchars($backtrace[1]['class']) . '::' : '';
$function = !empty($backtrace[1]['function']) ? htmlspecialchars($backtrace[1]['function']) . '() ' : '';
$output .= "<b>$class$function =>$file #$line</b><pre>";
ob_start();
foreach ($args as $arg) {
var_dump($arg);
}
$output .= htmlspecialchars(ob_get_contents(), ENT_COMPAT, 'UTF-8');
ob_end_clean();
$output .= '</pre>';
}
?>
usage :
<?php
dbug($scalar, $array, $object, $resource, CONSTANT);
dbug($other);
echo dbug('print'); ?>
I found it handy not to directly output result data because this makes it possible to debug variables before headers are sent (useful for pre sessions start code for example).