当只显示一个空白页面时,如何在Linux下调试PHP代码


How to debug PHP code under Linux when just an blank page is shown?

我是PHP新手。以下是我的情况:我在vim中编写代码并将其放在/var/www/下,然后我访问此以在浏览器上运行我的代码:

http://localhost/***.php

当我的代码有错误时,它显示一个空白页。

我如何调试我的代码像c++或Java?

谢谢。

编辑:

一些朋友提供的链接对我没有帮助。我用的是Linux。

在代码中包含这两行,以查看是哪种错误。

<?php 
       ini_set("display_errors",1);
       error_reporting(E_ALL);
        //code goes here
?>

对于更高级的解决方案,您可以使用PHP的XDebug扩展。

默认情况下,当加载XDebug时,它应该在出现任何致命错误时自动向您显示回溯。或者您跟踪到文件(xdebug.auto_trace)以对整个请求进行非常大的反向跟踪,或者执行分析(xdebug.profiler_enable)或其他设置。如果跟踪文件太大,可以使用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.

如果在本地主机上,我建议使用firefox或chrome并为mozilla安装firebug,而chrome获得默认值。请确保在本地主机上,您的设置与您要上传的服务器匹配,因为这可能会在上线时导致问题。

特别是大多数共享主机都将PHP设置为安全模式并关闭输出缓冲,所以如果您使用它,请通过调用ob_start()来使用它;等等,否则你应该没有问题,学习调试是乐趣的一部分,可以帮助你学到很多:)

对于php错误,只需重新编辑php.ini文件,您可以在http://php.net上找到所有相关信息

快乐编码

您可以在代码顶部使用error_reporting()

error_reporting(E_ALL);

你也需要在php.ini中打开display_errors

注意,在生产环境中应该有面向公众的错误报告。

虽然我个人认为var_dump对我的php调试足够了,但有些人倾向于使用像xdebug这样的调试器来做这件事。

每当出现问题时,PHP都会在其目录中生成一个error_log文件,您可以在其中找到调试信息。

同样,尝试使用var_dump($someVarible)。这将为您提供有关变量当前状态的有用信息——通常比echo更好。

除了关于在代码顶部使用error_reporting(E_ALL);的注释外,我喜欢使用php cli实用程序。我的电脑上没有web服务器,我用它来编写和调试php和html,所以cli实用程序的一个很好的功能是内置的web服务器。安装php命令行:

sudo apt install php7.0-cli

使用web服务器,cd到你的html和php文件所在的目录,并运行:

php -S localhost:8080

然后将浏览器指向你正在测试的文件…例如:

http://localhost:8080/test.php 

您也可以使用cli实用程序来运行php代码,它将按行号显示错误(使用显示行号的编辑器)。你可能需要注释掉并调整依赖于html调用它的代码。

php test.php

使用以下两行进行调试(这将启用查找行号上的错误):

ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);

可以查看PHP输出的日志文件。查看配置的一个好方法是使用phpinfo()。

还可以设置error_reporting(),这样您就可以看到错误消息而不是白页。

在大多数情况下,如果您将error_report设置为-1,您将能够在浏览器中看到所有通知,警告和错误。

有很多选择:

  • 使用error_reporting()函数让代码报告哪怕是最轻微的警告。或者查看服务器的错误日志文件。
  • 使用var_dump(), print_r()和echo来查看你的变量。
  • 使用debug_backtrace()和debug_print_backtrace()来查看你的调用堆栈
  • 使用xdebug。Xdebug通过使用断点和逐步执行代码、跟踪和分析来提供调试。Xdebug是一个PHP扩展。您可以通过在命令行、环境变量或ini文件中输入命令来独立运行它。
  • 更用户友好的是使用程序与Xdebug接口。默认情况下,该接口通过端口9003(在旧的Xdebug版本中为9000)。用于此目的的流行程序是PHPStorm, Netbeans和Visual Studio。

注意Xdebug会严重降低服务器的速度。所以不用的时候就把它关掉