如何调试PHP脚本?

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

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


当前回答

当不能使用Rails时,我经常使用CakePHP。为了调试错误,我通常会在tmp文件夹中找到error.log,然后在终端中使用命令…

tail -f app/tmp/logs/error.log

它可以让你从蛋糕中运行正在发生的事情的对话框,这非常方便,如果你想在代码中输出一些东西,你可以使用。

$this->log('xxxx');

这通常可以让你很好地了解发生了什么。

其他回答

在某种程度上,这取决于事情的走向。这是我尝试分离的第一件事,然后在必要时使用echo/print_r()。

注:你们知道你可以把true作为第二个参数传递给print_r(),它会返回输出而不是打印它吗?例如:

echo "<pre>".print_r($var, true)."</pre>";

XDebug对于开发至关重要。我安装它之前任何其他扩展。它为您提供任何错误的堆栈跟踪,您可以轻松地启用分析。

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

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

对于一个真正的调试环境,我发现最好的是Komodo IDE,但它要花费$$。

我使用Netbeans的XDebug和Easy XDebug FireFox插件

The add-on is essential when you debug MVC projects, because the normal way XDebug runs in Netbeans is to register the dbug session via the url. With the add-on installed in FireFox, you would set your Netbeans project properties -> Run Configuratuion -> Advanced and select "Do Not Open Web Browser" You can now set your break points and start the debugging session with Ctrl-F5 as usual. Open FireFox and right-click the Add-on icon in the right bottom corner to start monitoring for breakpoints. When the code reaches the breakpoint it will stop and you can inspect your variable states and call-stack.

手动调试对我来说通常更快——var_dump()和debug_print_backtrace()是你需要用来武装你的逻辑的所有工具。

For the really gritty problems that would be too time consuming to use print_r/echo to figure out I use my IDE's (PhpEd) debugging feature. Unlike other IDEs I've used, PhpEd requires pretty much no setup. the only reason I don't use it for any problems I encounter is that it's painfully slow. I'm not sure that slowness is specific to PhpEd or any php debugger. PhpEd is not free but I believe it uses one of the open-source debuggers (like XDebug previously mentioned) anyway. The benefit with PhpEd, again, is that it requires no setup which I have found really pretty tedious in the past.