PHP 的调试

Table of Contents

User Contributed Notes

Bladimir 28-Sep-2017 11:24
Following example above using variable-lenght arguments to support similar Web API's console.log implementation

<?php
function console_log( ...$messages ){
 
$msgs = '';
  foreach (
$messages as $msg) {
   
$msgs .= json_encode($msg);
  }

  echo
'<script>';
  echo
'console.log('. json_encode($msgs) .')';
  echo
'</script>';
}

?>
netyou 27-Sep-2015 04:29
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]
skatebiker at gmail dot com 05-Dec-2014 10:24
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.

[code]
@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

[/code]
skunkbad 17-Feb-2014 03:47
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.
Anonymous 02-Nov-2011 10:36
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.
dcz at phpbb-seo dot com 18-Mar-2011 11:32
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 =&gt;$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).
turaz dot w4l at gmail dot com 06-Aug-2009 01:14
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>';
}
?>
Diego 18-Feb-2009 01:21
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.
online _ use _ only == hotmail.com 16-Mar-2006 12:41
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).