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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4640421/
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-05 02:25:08  来源:igfitidea点击:

I need to debug PHP. What is my best choice?

phplinuxdebuggingvim

提问by Artemix

I'm working on a web site under a Linux environment (Debian). I'm not an expert at Linux but I can handle it, and the website is made using PHP, MySQL, HTML, etc.

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

The thing is, I'm using PHP for the server side. Right now, for testing, I installed Apache on my PC so I can test everything. But, it would be great if I could debug the PHP code. So far, I didn't need it, but now the code is getting larger, it's a must.

问题是,我在服务器端使用 PHP。现在,为了测试,我在我的 PC 上安装了 Apache,以便我可以测试所有内容。但是,如果我可以调试 PHP 代码就太好了。到目前为止,我还不需要它,但是现在代码越来越大,这是必须的。

So far, I'm using vim and everything is fine, but, how can I debug PHP in my case? What tools should I install? Are they free?

到目前为止,我正在使用 vim 并且一切正常,但是,在我的情况下如何调试 PHP?我应该安装什么工具?他们免费吗?

Basically, I need to know what would be the best choice in my situation.

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

采纳答案by greg0ire

XDebug provides step-by-step debugging, and can be used with eclipse PDT, netbeans and even vim. You really should give it a try. There also is Zend Debugger.

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

回答by kenorb

You can install PHP IDE with debugging facilities. It will help you to debug your PHP code step-by-step.

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

Few popular which has this feature:

很少有流行的具有此功能的:



For more advanced solution, you can install XDebugextension manually for PHP.

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

By default when XDebug is loaded, it should show you automatically the backtrace in case of any fatal error. Or you trace into file (xdebug.auto_trace) to have a very big backtrace of the whole request or do the profiling (xdebug.profiler_enable) or other settings. If the trace file is too big, you can use xdebug_start_trace() and xdebug_stop_trace() to dump the partial trace.

默认情况下,当加载 XDebug 时,它应该会自动向您显示任何致命错误的回溯。或者您跟踪到文件 (xdebug.auto_trace) 以对整个请求进行非常大的回溯,或者进行分析 (xdebug.profiler_enable) 或其他设置。如果跟踪文件太大,您可以使用 xdebug_start_trace() 和 xdebug_stop_trace() 转储部分跟踪。

Installation

安装

Using PECL:

使用 PECL:

pecl install xdebug

On Linux:

在 Linux 上:

sudo apt-get install php5-xdebug

On Mac (with Homebrew):

在 Mac 上(使用 Homebrew):

brew tap josegonzalez/php
brew search xdebug
php53-xdebug

Example of mine configuration:

我的配置示例:

[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.

回答by Bharat Khatri

If you're using apache as your web server, then you could use the apache logs to view any errors preventing the successful execution of a PHP script.

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

You could use

你可以用

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

to view the apache logs and that would do the job (atleast for a subset of PHP related errors).

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

回答by sameers

Specifically, if you're (a newbie like me that found this SO thread after starting out with PHP for the first time and are) trying to figure out what generates the error message in the first place, start with error_log. That's the function that sends a string as "an error message to the web server's error log or to a file."

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

In general, to learn the fundamentals of error reporting and configuration in PHP, start with the functions in the Error Handling Functions documentation on PHP.net- the comment on that page by petrov dot michael () gmail comis a helpful place to start.

一般来说,要学习 PHP 中错误报告和配置的基础知识,请从PHP.net 上错误处理函数文档中的函数开始- 该页面上的评论petrov dot michael () gmail com是一个有用的起点。

To actually generate the error, your best option as a newbie (meaning, least amount of documentation to read) is to start with print_r. Setting the second argument of print_rto TRUEwill return a string, that you can then pass to error_log. This is probably the route that will give you the fastest way to debug, given that your PHP code might be sitting inside some framework that adds in various output control layers (like Wordpress.)

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

回答by Tekz

If your PC OS is Windows then easiest way is to use CodeLobster free edition with any web development stack (WAMP, XAMPP) I found that setting up CodeLobster for debugging is very easy (compared to eclipse or netbeans) and CodeLobster installation is very small compared to other IDEs as well, moreover its free version support full debugging functionality.

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

You can find detailed step by step guide onhow to setup it in following post How to debug PHP - Easy way

您可以在以下文章如何调试 PHP - 简单方法中找到有关如何设置它的详细分步指南