如何调试PHP脚本?

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

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


当前回答

由Derick Rethans编写的Xdebug非常好。我前段时间用过,发现安装起来不那么容易。一旦你完成了,你就不会明白没有它你是如何做到的:-)

在Zend开发者专区有一篇很好的文章(在Linux上安装似乎并不容易),甚至还有一个Firefox插件,我从来没用过。

其他回答

Komodo IDE works well with xdebug, even for the remore debugging. It needs minimum amount of configuration. All you need is a version of php that Komodo can use locally to step through the code on a breakpoint. If you have the script imported into komodo project, then you can set breakpoints with a mouse-click just how you would set it inside eclipse for debugging a java program. Remote debugging is obviously more tricky to get it to work correctly ( you might have to map the remote url with a php script in your workspace ) than a local debugging setup which is pretty easy to configure if you are on a MAC or a linux desktop.

在生产环境中,我使用error_log()将相关数据记录到服务器的错误日志中。

根据问题的不同,我喜欢将error_reporting(E_ALL)与echo测试混合使用(以找到最初发生错误的违规行/文件;你知道它并不总是行/文件php告诉你对吗?),IDE大括号匹配(解决“解析错误:语法错误,意外的$end”问题),和print_r();退出;转储(真正的程序员查看源代码;p)。

你也不能用“memory_get_usage();”和“memory_get_peak_usage();”来打败phpdebug(检查sourceforge)来找到问题区域。

我使用内置调试器的zend studio for eclipse。与使用eclipse pdt和xdebug进行调试相比,它仍然很慢。希望他们能解决这些问题,在最近的版本中,速度有所提高,但仍然需要2-3秒。 zend firefox工具栏让事情变得非常简单(调试下一页、当前页等)。此外,它还提供了一个分析器,可以对代码进行基准测试,并提供饼状图、执行时间等。

通过简单地var_dump一些关键变量,可以很容易地找到大多数错误,但这显然取决于您开发的应用程序类型。

对于更复杂的算法,步进/断点/观察函数是非常有用的(如果没有必要)