我需要调试PHP。什么是我最好的选择?

时间:2011-01-09 16:59:59

标签: php linux debugging vim

我正在Linux环境下的网站上工作(Debian)。我不是Linux的专家,但我可以处理它,网站是使用PHP,MySQL,HTML等制作的。

问题是,我在服务器端使用PHP。现在,为了测试,我在我的PC上安装了Apache,所以我可以测试一切。但是,如果我可以调试PHP代码那将是很好的。到目前为止,我并不需要它,但现在代码越来越大,这是必须的。

到目前为止,我正在使用vim,一切都很好,但是,我怎样才能在我的情况下调试PHP?我应该安装什么工具?他们有空吗?

基本上,我需要知道在我的情况下最好的选择。

5 个答案:

答案 0 :(得分:6)

XDebug提供逐步调试,可以与eclipse PDT,netbeans甚至vim一起使用。你真的应该试一试。还有Zend Debugger。

答案 1 :(得分:2)

您可以使用调试工具安装PHP IDE。它将帮助您逐步调试PHP代码。

很少有这个功能受欢迎:


对于更高级的解决方案,您可以手动为PHP安装XDebug扩展名。

默认情况下,加载XDebug时,如果出现任何致命错误,它应自动显示回溯。或者您追踪到文件(xdebug.auto_trace)以获得整个请求的非常大的回溯或进行性能分析(xdebug.profiler_enable)或other settings。如果跟踪文件太大,您可以使用xdebug_start_trace()和xdebug_stop_trace()来转储部分跟踪。

<强>安装

使用PECL:

pecl install xdebug

在Linux上:

sudo apt-get install php5-xdebug

在Mac上(使用Homebrew):

brew tap josegonzalez/php
brew search xdebug
php53-xdebug

矿井配置示例:

[xdebug]

; Extensions
extension=xdebug.so
; zend_extension="/YOUR_PATH/php/extensions/no-debug-non-zts-20090626/xdebug.so"
; zend_extension="/Applications/MAMP/bin/php/php5.3.20/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so" ; MAMP

; Data
xdebug.show_exception_trace=1       ; bool: Show a stack trace whenever an exception is raised.
xdebug.collect_vars = 1             ; bool: Gather information about which variables are used in a certain scope.
xdebug.show_local_vars=1            ; int: Generate stack dumps in error situations.
xdebug.collect_assignments=1        ; bool: Controls whether Xdebug should add variable assignments to function traces.
xdebug.collect_params=4             ; int1-4: Collect the parameters passed to functions when a function call is recorded.
xdebug.collect_return=1             ; bool: Write the return value of function calls to the trace files.
xdebug.var_display_max_children=256 ; int: Amount of array children and object's properties are shown.
xdebug.var_display_max_data=1024    ; int: Max string length that is shown when variables are displayed.
xdebug.var_display_max_depth=3      ; int: How many nested levels of array/object elements are displayed.
xdebug.show_mem_delta=0             ; int: Show the difference in memory usage between function calls.

; Trace
xdebug.auto_trace=0                 ; bool: The tracing of function calls will be enabled just before the script is run.
xdebug.trace_output_dir="/var/log/xdebug" ; string: Directory where the tracing files will be written to.
xdebug.trace_output_name="%H%R-%s-%t"     ; string: Name of the file that is used to dump traces into.

; Profiler
xdebug.profiler_enable=0            ; bool: Profiler which creates files read by KCacheGrind.
xdebug.profiler_output_dir="/var/log/xdebug"  ; string: Directory where the profiler output will be written to.
xdebug.profiler_output_name="%H%R-%s-%t"      ; string: Name of the file that is used to dump traces into.
xdebug.profiler_append=0            ; bool: Files will not be overwritten when a new request would map to the same file.

; CLI
xdebug.cli_color=1                  ; bool: Color var_dumps and stack traces output when in CLI mode.

; Remote debugging
xdebug.remote_enable=off            ; bool: Try to contact a debug client which is listening on the host and port.
xdebug.remote_autostart=off         ; bool: Start a remote debugging session even GET/POST/COOKIE variable is not present.
xdebug.remote_handler=dbgp          ; select: php3/gdb/dbgp: The DBGp protocol is the only supported protocol.
xdebug.remote_host=localhost        ; string: Host/ip where the debug client is running.
xdebug.remote_port=9000             ; integer: The port to which Xdebug tries to connect on the remote host.
xdebug.remote_mode=req              ; select(req,jit): Selects when a debug connection is initiated.
xdebug.idekey="xdebug-cli"          ; string: IDE Key Xdebug which should pass on to the DBGp debugger handler.
xdebug.remote_log="/var/log/xdebug.log" ; string: Filename to a file to which all remote debugger communications are logged.

答案 2 :(得分:0)

如果您使用apache作为Web服务器,那么您可以使用apache日志查看阻止成功执行PHP脚本的任何错误。

您可以使用

tail -f /var/log/apache2/error.log

查看apache日志,这将完成工作(至少与PHP相关的错误子集)。

答案 3 :(得分:0)

具体来说,如果你(像我这样的新手在第一次开始使用PHP之后找到了这个SO线程并且正在尝试找出首先产生错误消息的内容),请从{{3开始}}。这是将字符串作为“错误消息发送到Web服务器的错误日志或文件”的函数。

一般而言,要了解PHP中错误报告和配置的基础知识,请从error_log中的函数开始 - petrov dot michael () gmail com对该页面的评论是一个有用的起点。

要实际生成错误,作为新手的最佳选择(意思是,要阅读的文档数量最少)是从Error Handling Functions documentation on PHP.net开始。将print_r的第二个参数设置为TRUE将返回一个字符串,然后您可以将其传递给error_log。这可能是为您提供最快调试方式的路径,因为您的PHP代码可能位于某个框架内,该框架会添加各种输出控制层(如Wordpress)。

答案 4 :(得分:0)

如果您的PC操作系统是Windows,那么最简单的方法是将CodeLobster免费版与任何Web开发堆栈(WAMP,XAMPP)一起使用我发现设置CodeLobster进行调试非常容易(与eclipse或netbeans相比)并且CodeLobster安装是与其他IDE相比非常小,而且它的免费版本支持完整的调试功能。

您可以在以下帖子中找到有关如何设置的详细分步指南 How to debug PHP - Easy way

相关问题