php 如何调试Php代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5710665/
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
How to debug Php code?
提问by Don Lun
I am newbie on PHP. Here is my situation. I write my code in vim and put it under /var/www/ then I can use
我是 PHP 新手。这是我的情况。我在 vim 中编写我的代码并将其放在 /var/www/ 下然后我可以使用
localhost/*.php
本地主机/ *.php
to run my code on browser.
在浏览器上运行我的代码。
When my code has bug. It just come out nothing.
当我的代码有错误时。它只是什么都没有出现。
How can I debug mu code like c++ or java?
如何调试像 c++ 或 java 这样的 mu 代码?
Thanks.
谢谢。
Edited:
编辑:
The link some friends provide is not helpful for me. I am under Linux. That's for Win.
一些朋友提供的链接对我没有帮助。我在Linux下。那是为了赢。
采纳答案by Version1
If on a localhost, I would suggest using firefox or chrome and installing firebug for mozilla, and chrome gets a default. Be sure that on a local host your settings are matched to the server you are uploading to as this can cause problems when going live.
如果在本地主机上,我建议使用 firefox 或 chrome 并为 mozilla 安装 firebug,chrome 获得默认值。确保在本地主机上,您的设置与您上传到的服务器相匹配,因为这可能会导致上线时出现问题。
Specifically most shared hosting has PHP on safe mode and output buffering off, so if you use it, use it by calling it by calling ob_start(); etc, otherwise you should have no problems, and learning to debug is part of the fun, helps you learn alot :)
特别是大多数共享主机都在安全模式下使用 PHP 并关闭输出缓冲,因此如果您使用它,请通过调用 ob_start() 调用它来使用它;等等,否则你应该没有问题,学习调试是乐趣的一部分,可以帮助你学到很多东西:)
As for php errors just re-edit your php.ini file, you can find al relevant information on http://php.net
至于php错误只需重新编辑你的php.ini文件,你可以在http://php.net上找到所有相关信息
Happy Coding
快乐编码
回答by kenorb
For more advanced solution, you can use XDebugextension 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 时,它应该会自动向您显示任何致命错误的回溯。或者您跟踪到 file( 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 ThisisFish
include this both line in code to see what kind of error is.
在代码中包含这两行以查看错误类型。
<?php
ini_set("display_errors",1);
error_reporting(E_ALL);
//code goes here
?>
回答by alex
You can use error_reporting()
at the top of your code...
您可以error_reporting()
在代码顶部使用...
error_reporting(E_ALL);
You will also want display_errors
on in php.ini
.
你也会想要display_errors
在php.ini
.
Note that you should have public facing error reporting off in a production environment.
请注意,您应该在生产环境中关闭面向公众的错误报告。
回答by Spyros
Though i personally find var_dump just enough for my php debugging, some people tend to like using debuggers like xdebug to do so.
虽然我个人认为 var_dump 刚好用于我的 php 调试,但有些人倾向于使用像 xdebug 这样的调试器来这样做。
回答by Ken H
In addition to the comments about using error_reporting(E_ALL);
at the top of your code, I like to use the php cli utility. I don't have a web server on the pc that I use to write and debug php and html, so a nice feature of the cli utility is the built-in web server. To install the php cli:
除了error_reporting(E_ALL);
代码顶部关于 using 的注释之外 ,我还喜欢使用 php cli 实用程序。我在 PC 上没有用于编写和调试 php 和 html 的 Web 服务器,因此 cli 实用程序的一个很好的功能是内置的 Web 服务器。要安装 php cli:
sudo apt install php7.0-cli
To use the web server, cd to the directory where your html and php files are located and run:
要使用 Web 服务器,请 cd 到您的 html 和 php 文件所在的目录并运行:
php -S localhost:8080
Then point your browser to the files you're testing...for example:
然后将浏览器指向您正在测试的文件...例如:
http://localhost:8080/test.php
You can also use the cli utility to run your php code, and it will display the errors by line number (use an editor that displays line numbers). You may need to comment out and adjust code that depends on html calling it.
您还可以使用 cli 实用程序运行您的 php 代码,它将按行号显示错误(使用显示行号的编辑器)。您可能需要注释和调整依赖于 html 调用它的代码。
php test.php
回答by Nick Brunt
PHP produces an error_log
file in its directory whenever a problem occurs, you can find debug information there.
error_log
每当出现问题时,PHP都会在其目录中生成一个文件,您可以在那里找到调试信息。
Also, try using var_dump($someVarible)
. This will give you useful information about the current state of a variable - often better than echo
.
另外,尝试使用var_dump($someVarible)
. 这将为您提供有关变量当前状态的有用信息 - 通常比echo
.
回答by Felipe Almeida
In most cases, if you set you error_report
to -1 you'll be able to see all notices, warning and errors in your browser.
在大多数情况下,如果将您设置error_report
为 -1,您将能够在浏览器中看到所有通知、警告和错误。
回答by nikhilmeth
Use these two lines to debug (this will enable to find the errors on the line numbers):
使用这两行进行调试(这将能够找到行号上的错误):
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
回答by Jason McCreary
You can check the log file output by PHP. A good way to see your configuration is to use phpinfo().
您可以检查 PHP 输出的日志文件。查看配置的一个好方法是使用phpinfo()。
Also you can set error_reporting()so you can see the error message instead of a white page.
您也可以设置error_reporting()以便您可以看到错误消息而不是白页。