你如何调试 PHP 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:07:33  来源:igfitidea点击:

How do you debug PHP scripts?

phpeclipsedebuggingphpstormxdebug

提问by Marcel

How do you debug PHPscripts?

你如何调试PHP脚本?

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

我知道基本的调试,例如使用错误报告。PHPEclipse 中的断点调试也很有用。

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

在 phpStorm 或任何其他 IDE 中调试的最佳(就快速和简单而言)方法是什么?

采纳答案by John Downey

Try Eclipse PDTto 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.

尝试使用Eclipse PDT来设置具有您提到的调试功能的 Eclipse 环境。与 var_dump 的旧方法相比,进入代码的能力是一种更好的调试方式,并在各个点打印以查看您的流程哪里出错了。当所有其他方法都失败并且我所拥有的只是 SSH 和 vim 时,我仍然var_dump()/die()要找到代码向南的位置。

回答by Pat

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

您可以使用 Firephp 插件来调试 php,并在与 javascript 相同的环境中调试 php。

I also use Xdebugmentioned earlier for profiling php.

我还使用前面提到的Xdebug来分析 php。

回答by eisberg

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;
        }
    }
}

回答by djn

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

Xdebug 和用于 Notepad++ 的 DBGp 插件用于重型 bug 搜寻,FirePHP 用于轻量级的东西。又快又脏?没有什么能比得上 debug 了

回答by Julio César

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

XDebug对于开发是必不可少的。我在任何其他扩展之前安装它。它为您提供任何错误的堆栈跟踪,您可以轻松启用分析。

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.

要快速查看数据结构,请使用var_dump(). 不要使用,print_r()因为你必须用<pre>它包围它,而且它一次只打印一个 var。

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

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

对于真正的调试环境,我发现最好的是Komodo IDE,但它的成本是 $$。

回答by monk.e.boy

PhpEd is really good. You can step into/over/out of functions. You can run ad-hoc code, inspect variables, change variables. It is amazing.

PhpEd 真的很好。您可以进入/跳过/退出功能。您可以运行临时代码、检查变量、更改变量。这是惊人的。

回答by jlleblanc

1) I use print_r(). In TextMate, I have a snippet for 'pre' which expands to this:

1)我使用print_r()。在 TextMate 中,我有一个“pre”的片段,它扩展为:

echo "<pre>";
print_r();
echo "</pre>";

2) I use Xdebug, but haven't been able to get the GUI to work right on my Mac. It at least prints out a readable version of the stack trace.

2) 我使用 Xdebug,但无法让 GUI 在我的 Mac 上正常工作。它至少打印出堆栈跟踪的可读版本。

回答by Teifion

In all honesty, a combination of print and print_r() to print out the variables. I know that many prefer to use other more advanced methods but I find this the easiest to use.

老实说,结合 print 和 print_r() 来打印出变量。我知道很多人更喜欢使用其他更高级的方法,但我发现这是最容易使用的。

I will say that I didn't fully appreciate this until I did some Microprocessor programming at Uni and was not able to use even this.

我会说,直到我在 Uni 进行了一些微处理器编程并且甚至无法使用它之前,我才完全理解这一点。

回答by Michael Stum

I've used the Zend Studio (5.5), together with Zend Platform. That gives proper debugging, breakpoints/stepping over the code etc., although at a price.

我使用过Zend Studio (5.5)Zend Platform。这提供了适当的调试、断点/单步执行代码等,尽管是有代价的。

回答by Christian Lescuyer

Xdebug, by Derick Rethans, is very good. I used it some time ago and found it was not so easy to install. Once you're done, you won't understand how you managed without it :-)

Xdebug,由 Derick Rethans 编写,非常好。前段时间用过,发现不是那么容易安装。完成后,您将无法理解没有它您是如何管理的 :-)

There is a good article on Zend Developer Zone(installing on Linux doesn't seem any easier) and even a Firefox plugin, which I never used.

Zend Developer Zone上有一篇很好的文章(在 Linux 上安装似乎并不容易),甚至还有一个我从未使用过的Firefox 插件