PHP 5.6.30 Released

Debugging in PHP

Table of Contents

add a note add a note

User Contributed Notes 8 notes

1 year 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>';

$myvar = array(1,2,3);
console_log( $myvar ); // [1,2,3]
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.
5 years ago
If anyone's trying to actually set up the official debugger from Zend ( 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.
online _ use _ only ==
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

skatebiker at gmail dot com
2 years 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.

// 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

dcz at phpbb-seo dot com
5 years ago
Here my little contribution for a simple yet handy debug function :
* dbug (mixed $expression [, mixed $expression [, $... ]])
* Author : dcz
* Feel free to use as you wish at your own risk ;-)
function dbug() {
$output = '', $doc_root;
$args = func_get_args();
    if (!empty(
$args) && $args[0] === 'print') {
$_output = $output;
$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>";
    foreach (
$args as $arg) {
$output .= htmlspecialchars(ob_get_contents(), ENT_COMPAT, 'UTF-8');
$output .= '</pre>';

usage :
($scalar, $array, $object, $resource, CONSTANT);
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
7 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:

function debug_view ( $what ) {
    if (
is_array( $what ) )  {
print_r ( $what );
    } else {
var_dump ( $what );
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