Отладка в PHP

Содержание

Коментарии

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).
2006-03-15 17:41:19
http://php5.kiev.ua/manual/ru/debugger.html
Автор:
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.
2009-02-18 06:21:38
http://php5.kiev.ua/manual/ru/debugger.html
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>';
}
?>
2009-08-06 07:14:31
http://php5.kiev.ua/manual/ru/debugger.html
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$resourceCONSTANT);
//.. 
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).
2011-03-18 04:32:50
http://php5.kiev.ua/manual/ru/debugger.html
Автор:
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.
2011-11-02 15:36:14
http://php5.kiev.ua/manual/ru/debugger.html
Автор:
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.
2014-02-16 20:47:57
http://php5.kiev.ua/manual/ru/debugger.html
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]
2014-12-05 15:24:17
http://php5.kiev.ua/manual/ru/debugger.html
Автор:
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]
2015-09-27 10:29:08
http://php5.kiev.ua/manual/ru/debugger.html
Автор:
I would like to add http://phpdebugbar.com/ which is also a handy tool for debugging/profiling data. and you could use it whenever framework you are using. :-)
2017-04-19 11:17:11
http://php5.kiev.ua/manual/ru/debugger.html

    Поддержать сайт на родительском проекте КГБ