PHP 7.0.6 Released

Debugging in PHP

Table of Contents

User Contributed Notes

netyou
7 months ago
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]
online _ use _ only == hotmail.com
10 years ago
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).
skatebiker at gmail dot com
1 year ago
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]
Anonymous
4 years ago
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
5 years ago
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).
skunkbad
2 years ago
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.
turaz dot w4l at gmail dot com
6 years ago
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
7 years ago
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.
To Top