note 102982 added to debugger
| From: | dcz at phpbb-seo dot com | Date: | Fri, 18 Mar 2011 10:32:50 +0000 |
| Subject: | note 102982 added to debugger | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177973@lists.php.net to get a copy of this message | ||
Here my little contribution for a simple yet handy debug function :
<?php
/**
* dbug (mixed $expression [, mixed $expression [, $... ]])
* Author : dcz
* Feel free to use as you wish at your own risk ;-)
*/
function dbug() {
static $output = '', $doc_root;
$args = func_get_args();
if (!empty($args) && $args[0] === 'print') {
$_output = $output;
$output = '';
return $_output;
}
// do not repeat the obvious (matter of taste)
if (!isset($doc_root)) {
$doc_root = str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']);
}
$backtrace = debug_backtrace();
// you may want not to htmlspecialchars here
$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'); // actually output the result of all previous calls
// looks like :
// class::method() =>/path/from/doc/root/file.php #line
// var_dump result
?>
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).
----
Server IP: 69.147.83.197
Probable Submitter: 78.226.240.47
----
Manual Page -- http://www.php.net/manual/en/debugger.php
Edit -- https://master.php.net/note/edit/102982
Del: integrated -- https://master.php.net/note/delete/102982/integrated
Del: useless -- https://master.php.net/note/delete/102982/useless
Del: bad code -- https://master.php.net/note/delete/102982/bad+code
Del: spam -- https://master.php.net/note/delete/102982/spam
Del: non-english -- https://master.php.net/note/delete/102982/non-english
Del: in docs -- https://master.php.net/note/delete/102982/in+docs
Del: other reasons-- https://master.php.net/note/delete/102982
Reject -- https://master.php.net/note/reject/102982
Search -- https://master.php.net/manage/user-notes.php