©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
[#1] netyou [2015-09-27 07:29:08]
I find it very useful to print out to the browsers console instead of just var_dumping:
function console_log( $data ){
echo '<script>';
echo 'console.log('. json_encode( $data ) .')';
echo '</script>';
}
Usage:
$myvar = array(1,2,3);
console_log( $myvar ); // [1,2,3]
[#2] skatebiker at gmail dot com [2014-12-05 13:24:17]
p_r() is a function for logging variable values.
In this example the function p_r() does only log when the URL parameter d=<nonzero> is set. Reset it by d=0.
When the parameter is a valid filename (relative to the script's path) it will be logged to that file rather than to the browser.
@session_start();
// debug
if (isset($_GET['d']))
{
$_SESSION['d'] = $_GET['d'];
}
if (@$_SESSION['d']) {
function p_r($exp)
{
$res = "";
$trace = debug_backtrace();
$level = 0;
$e = error_reporting(E_ALL&~E_NOTICE);
$file = strrpos($trace[$level]['file'], "/");
$file = substr($trace[$level]['file'],$file+1);
$line = date("H:i:s"). " " . $file . ' ' . $trace[$level]['line'] . ' ' . $trace[$level+1]['function'] . "()";
$e = error_reporting($e);
if (!is_string($exp)) $exp = var_export($exp,1);
if (substr($_SESSION["d"],-4)==".log") {
file_put_contents ($_SESSION["d"],$line . ": ". $exp . "\n", FILE_APPEND);
} else {
$res = $line . "\n<pre>".htmlentities($exp). "</pre>\n";
echo $res;
}
return $res;
}
// refresh to prevent timeout
$a = $_SESSION['d'];
$_SESSION['d'] = $a;
error_reporting (E_ALL);
} else {
function p_r() {}
} // end if debug
[#3] skunkbad [2014-02-16 18:47:57]
I am a firm believer in the Firephp debugger. It works with Firefox and Firebug to allow you to see the value of any string, array, or object. The best part of it is that it will not interrupt the actual browser output, so you can see the output as it was intended to be seen.
For those who prefer Google's Chome browser, there is something called ChromePHP which is similar, but the way Firephp displays the values in the console is better, plus Firebug itself is an almost priceless development tool.
So, if you're looking for a great debugger, check out Firephp. After you use it you will feel naked if it's not available.
[#4] Anonymous [2011-11-02 13:36:14]
If anyone's trying to actually set up the official debugger from Zend (http://www.zend.com/en/products/studio/downloads) with PHP 5.3.8, you'll notice the zip only contains the nts (non-thread-safe) version of the debugger for PHP 5.3.x. Try as you might, it just doesn't seem to work with the tread-safe version of PHP 5.3.8, so for Windows at least I found you'll also need to have the NON-THREAD-SAFE version of PHP installed.
[#5] dcz at phpbb-seo dot com [2011-03-18 02:32:50]
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;
}
// 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).
[#6] turaz dot w4l at gmail dot com [2009-08-06 04:14:31]
I usually use this simple function in combo with a die(); in order to have on screen the value of a variable or array:
<?php
function debug_view ( $what ) {
echo '<pre>';
if ( is_array( $what ) ) {
print_r ( $what );
} else {
var_dump ( $what );
}
echo '</pre>';
}
?>
[#7] Diego [2009-02-18 04:21:38]
If you don't find a syntax error, you can comment out a block where you assume the error (or put it out of the document by [ctrl] + [X], but keep a copy on your HD for the case, your computer crashes) and check, if the syntax error is still there.
If not, it must be anywhere in your commented text; if yes, it must be somewhere else.
If you want to locate the error better, do it again with an other and/or smaller piece of code, till you get it.
[#8] online _ use _ only == hotmail.com [2006-03-15 15:41:19]
I still find that printing out variable values at problem points in the code is one of the easiest ways for me to debug. If you're interested in knowing the full contents of an object/array/scalar, then use
var_dump($var).