How do you debug PHP scripts?

352,898

Solution 1

Try Eclipse PDT to setup an Eclipse environment that has debugging features like you mentioned. The ability to step into the code is a much better way to debug then the old method of var_dump and print at various points to see where your flow goes wrong. When all else fails though and all I have is SSH and vim I still var_dump()/die() to find where the code goes south.

Solution 2

You can use Firephp an add-on to firebug to debug php in the same environment as javascript.

I also use Xdebug mentioned earlier for profiling php.

Solution 3

This is my little debug environment:

error_reporting(-1);
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_BAIL, 0);
assert_options(ASSERT_QUIET_EVAL, 0);
assert_options(ASSERT_CALLBACK, 'assert_callcack');
set_error_handler('error_handler');
set_exception_handler('exception_handler');
register_shutdown_function('shutdown_handler');

function assert_callcack($file, $line, $message) {
    throw new Customizable_Exception($message, null, $file, $line);
}

function error_handler($errno, $error, $file, $line, $vars) {
    if ($errno === 0 || ($errno & error_reporting()) === 0) {
        return;
    }

    throw new Customizable_Exception($error, $errno, $file, $line);
}

function exception_handler(Exception $e) {
    // Do what ever!
    echo '<pre>', print_r($e, true), '</pre>';
    exit;
}

function shutdown_handler() {
    try {
        if (null !== $error = error_get_last()) {
            throw new Customizable_Exception($error['message'], $error['type'], $error['file'], $error['line']);
        }
    } catch (Exception $e) {
        exception_handler($e);
    }
}

class Customizable_Exception extends Exception {
    public function __construct($message = null, $code = null, $file = null, $line = null) {
        if ($code === null) {
            parent::__construct($message);
        } else {
            parent::__construct($message, $code);
        }
        if ($file !== null) {
            $this->file = $file;
        }
        if ($line !== null) {
            $this->line = $line;
        }
    }
}

Solution 4

Xdebug and the DBGp plugin for Notepad++ for heavy duty bug hunting, FirePHP for lightweight stuff. Quick and dirty? Nothing beats dBug.

Solution 5

XDebug is essential for development. I install it before any other extension. It gives you stack traces on any error and you can enable profiling easily.

For a quick look at a data structure use var_dump(). Don't use print_r() because you'll have to surround it with <pre> and it only prints one var at a time.

<?php var_dump(__FILE__, __LINE__, $_REQUEST); ?>

For a real debugging environment the best I've found is Komodo IDE but it costs $$.

Share:
352,898
Marcel
Author by

Marcel

Updated on August 04, 2020

Comments

  • Marcel
    Marcel almost 4 years

    How do you debug PHP scripts?

    I am aware of basic debugging such as using the Error Reporting. The breakpoint debugging in PHPEclipse is also quite useful.

    What is the best (in terms of fast and easy) way to debug in phpStorm or any other IDE?

  • Jahangir
    Jahangir over 15 years
    Its not just installing that's frustrating. Configuring Xdebug to work with Eclipse can be a nightmare. I was able to get Xdebug installed on CentOS 5 but EclipsePDT+Xdebug dont want to co-operate :(
  • agucho
    agucho over 15 years
    I am glad you mentioned print as well as print_r, I use a basic print to see if the code executed to a certain point, which helps isolating the problem.
  • jdelator
    jdelator over 15 years
    I just wrap that in a function called debug. So then I can do debug($var);
  • altermativ
    altermativ almost 15 years
    I'm sure you mean echo "</pre>"; at the end though.
  • Joshua K
    Joshua K over 14 years
    I use both print and var_dump(). I use print to display debug messages and information and var_dump to indicate the state of variables as things progress.
  • Artem Russakovskii
    Artem Russakovskii over 14 years
    The PHPEd debugger is actually written by the same guy who wrote PHPEd and I'm pretty sure it's not open source. At least PHPEd doesn't ship with the source but instead compiled .so's and .dll's.
  • DisgruntledGoat
    DisgruntledGoat almost 14 years
    You can also pass 'true' into the function so it returns the string. It means you can do this: echo '<pre>', print_r($var, true), '</pre>';
  • AlexMorley-Finch
    AlexMorley-Finch about 12 years
    You should use this function: kill( $data ) { die( var_dump ( $data ) ); } It saves typing 10 characters, best function i have ever written tbh:)
  • Sec
    Sec almost 11 years
    Thank you. That saved my day. (I just had to remove that E_STRICT)
  • Joe
    Joe almost 11 years
    The DBGp plugin doesn't work with the current version of notepad++/xdebug and there are no plans to fix it. You can see my discussion with the creator linked here
  • Arvind K.
    Arvind K. over 10 years
  • Arvind K.
    Arvind K. over 10 years
  • Arvind K.
    Arvind K. over 10 years
  • Arvind K.
    Arvind K. over 10 years
  • siliconrockstar
    siliconrockstar over 10 years
    I have used PhpEd and I have no kind words for it when compared to a real IDE like NetBeans or Eclipse, nor does this comment add anything useful to the question. -1
  • Tomáš Fejfar
    Tomáš Fejfar over 10 years
  • RPDeshaies
    RPDeshaies about 10 years
    Is there a way to beautify the "var_dump" ?
  • Dan
    Dan about 10 years
    @Tareck117 Yes. You can install xdebug
  • jason
    jason over 9 years
    @Tareck an alternative to prettify debug output : $v2 = array ("h"=>5, "o"=>10, "p"=>11); $str = serialize($v2); echo "<pre>" .print_r(unserialize($str), true) . "</pre>";
  • Mihaela Grigore
    Mihaela Grigore over 9 years
    And here's a quick guide to using FirePHP: sitepoint.com/debug-php-firebug-firephp
  • Konstantin
    Konstantin over 9 years
    @Tareck117 Yes, use var_export instead
  • Francisco Presencia
    Francisco Presencia over 9 years
    @AlexMorley-Finch I raise you to kill($data) { echo "<pre>"; var_dump($data); echo "</pre>"; exit; }
  • lm713
    lm713 almost 9 years
    I tried most of the IDEs (including Zend, NetBeans and Eclipse) before buying PhpED Professional because it was the best by a mile and a half. This was a few years ago, so the others may have since improved, but at the time most of them were painfully slow because they were written in Java. I don't understand how someone can have "no kind words for it" when (for me) it was so clearly the best, the decision was a no-brainer.
  • Madbreaks
    Madbreaks over 8 years
    assert_callcack heh
  • Gruber
    Gruber over 8 years
    Link is "recoverable" via the incredible Web Archive, last check as of 7 may '15.
  • Kind Contributor
    Kind Contributor about 8 years
    Don't bother with the Web Archive, since this answer was created Eclipse have created a proper PHP Development Environment - eclipse.org/pdt. I have updated the answer to suit.