php调试xdebug_PHP调试和分析– Xdebug

php调试xdebug

PHP Debugging and Profiling - Xdebug
PHP Debugging and Profiling - Xdebug

PHP Debugging and Profiling – Xdebug When we are talking about web development, PHP surely one of the most popular languages in the world. Its popularity can be judged from the fact that all the major CMS solutions available are powered by PHP. However for long PHP developers faced a problem in absence of a suitable debugger which other programming languages such as Java and C# enjoyed. Overcoming this meant manually adding debug statements which was quite a challenging task. This problem was solved once Xdebug hit the scenes and soon became one of the most popular PHP extensions.

PHP调试和分析– Xdebug当我们谈论Web开发时,PHP无疑是世界上最受欢迎的语言之一。 可以从所有可用的主要CMS解决方案均由PHP驱动这一事实来判断其受欢迎程度。 然而,长期以来,PHP开发人员在缺少合适的调试器时遇到了一个问题,而其他Java和C#等编程语言都无法使用它。 克服这一点意味着手动添加调试语句是一项艰巨的任务。 一旦Xdebug出现并很快成为最受欢迎PHP扩展之一,这个问题就解决了。

Xdebug

Xdebug的

Xdebug is a free and open source and offers PHP developers much more than just basic debugging support. It can be used for stack traces, profiling, code coverage etc. In this write-up we shall take a look at how we can install and configure Xdebug. Apart from this we shall also look at how we can debug and read a profiling report.

Xdebug是免费的开放源代码,它为PHP开发人员提供的不仅仅是基本的调试支持。 它可以用于堆栈跟踪,分析,代码覆盖等。在本文中,我们将研究如何安装和配置Xdebug。 除此之外,我们还将研究如何调试和读取性能分析报告。

安装 (Installation)

Installing Xdebug is extremely easy as it is available as PECL extension. In our example we shall be using debian/Ubuntu. This process may be slightly different for other Linux distributions. To install a PECL extension we need to install PEAR first.

安装Xdebug非常容易,因为它可以作为PECL扩展使用。 在我们的示例中,我们将使用debian / Ubuntu。 对于其他Linux发行版,此过程可能略有不同。 要安装PECL扩展,我们需要先安装PEAR。

To install a PECL extension you need to install PEAR first. Be sure that you have installed PEAR and continue then with installing the Xdebug PECL extension:

要安装PECL扩展,您需要先安装PEAR。 确保已安装PEAR,然后继续安装Xdebug PECL扩展:

Install PECL/PEAR for phpShell

为phpShell安装PECL / PEAR


sudo apt-get install php-pear
sudo apt-get install php5-dev

sudo apt-get install php-pear
sudo apt-get install php5-dev

Install Xdebug as PECL extensionShell

安装Xdebug作为PECL extensionShell


sudo pecl install Xdebug

sudo pecl install Xdebug

Configuration

组态

Once the installation is over you need to your php.ini and add the following line to the extension section:

安装结束后,您需要php.ini并将以下行添加到扩展部分:

php.ini

php.ini


zend_extension=/usr/lib/php5/{BUILDDATE}/xdebug.so

zend_extension=/usr/lib/php5/{BUILDDATE}/xdebug.so

Make sure you use ‘zend_extension’ instead of ‘extension’ and replace {BUILDDATE} with the date you found in your php5 lib folder.

确保使用“ zend_extension”而不是“ extension”,并将{BUILDDATE}替换为在php5 lib文件夹中找到的日期。

php.ini

php.ini


xdebug.remote_enable=1 # enable remote debugging for all hosts, which use this php.ini
xdebug.remote_handler=dbgp # debugger protocol, you may change this value to 'gdp' if your IDE is supporting only this
xdebug.remote_mode=req # Xdebug tries to connect to your IDE as soon as you start a script, you can choose 'jit' when Xdebug shall connect to our IDE only when an error condition occurs
xdebug.remote_port=9000 # default port for Xdebug
xdebug.remote_host=aaa.bbb.ccc.ddd # the IP of the remote debugger (your local IP)
xdebug.remote_autostart=0 # Xdebug does only start when you set GET/POST/COOKIE variable, if you set it to '1' Xdebug starts a debugging session on every script call

xdebug.remote_enable=1 # enable remote debugging for all hosts, which use this php.ini
xdebug.remote_handler=dbgp # debugger protocol, you may change this value to 'gdp' if your IDE is supporting only this
xdebug.remote_mode=req # Xdebug tries to connect to your IDE as soon as you start a script, you can choose 'jit' when Xdebug shall connect to our IDE only when an error condition occurs
xdebug.remote_port=9000 # default port for Xdebug
xdebug.remote_host=aaa.bbb.ccc.ddd # the IP of the remote debugger (your local IP)
xdebug.remote_autostart=0 # Xdebug does only start when you set GET/POST/COOKIE variable, if you set it to '1' Xdebug starts a debugging session on every script call

With these steps you have installed and configured Xdebug with a basic setup.

通过这些步骤,您已经使用基本设置安装并配置了Xdebug。

调试 (Debugging)

Now that we are done with the installation and configuration let us see how the debugging process works. You might be tempted to use the var_dump() and exit/die() combination for debugging but this process will require you to modify the code for debugging. You can bypass this problem in Xdebug as it lets you pause your application’s execution where ever you want. You can easily configure Netbeans to act as an Xdebug client. Open the options window (Tools > Options) and in the PHP section go to the debugging tab. Enter the debugging port given in php.ini and a Session ID as this allows you to run the debugger by clicking Debug in the tools tab.

现在我们已经完成了安装和配置,让我们看看调试过程是如何进行的。 您可能很想使用var_dump()和exit / die()组合进行调试,但是此过程将需要您修改代码以进行调试。 您可以在Xdebug中绕过此问题,因为它使您可以在任何需要的地方暂停应用程序的执行。 您可以轻松地将Netbeans配置为充当Xdebug客户端。 打开选项窗口(“工具”>“选项”),然后在“ PHP”部分转到“调试”选项卡。 输入php.ini中提供的调试端口和会话ID,因为您可以通过单击“工具”选项卡中的“调试”来运行调试器。

Now press the Debug button in the toolbar to start debugging. An application opens in a browser, and PHP’s execution will pause at the first line if you have enabled “Stop at first line” otherwise, it will run until it encounters the first breakpoint. Go to the next break point using the continue button. You will notice XDEBUG_SESSION_START parameter in the browser’s URL. To start the debugger you must pass XDEBUG_SESSION_START as a request parameter (GET/POST) or XDEBUG_SESSION as a cookie parameter. Further you can easily add breakpoints by clicking the line number in the editor’s margin and then pause execution on them.

现在按工具栏中的“调试”按钮开始调试。 在浏览器中打开一个应用程序,如果启用了“在第一行停止”,则PHP的执行将在第一行暂停,否则它将一直运行直到遇到第一个断点。 使用继续按钮转到下一个断点。 您会在浏览器的URL中注意到XDEBUG_SESSION_START参数。 要启动调试器,您必须传递XDEBUG_SESSION_START作为请求参数(GET / POST)或XDEBUG_SESSION作为cookie参数。 此外,您可以通过单击编辑器空白处的行号轻松添加断点,然后暂停执行。

剖析 (Profiling)

To optimize an application you must profile them. This records important details like the time it takes for statements and functions to execute or the number of times they are called. To make use of Xdebug as a profiling tool for PHP add the following settings to php.ini

要优化应用程序,您必须对其进行概要分析。 这将记录重要的细节,例如执行语句和函数所需的时间或调用它们的次数。 要将Xdebug用作PHP的性能分析工具,请将以下设置添加到php.ini中


xdebug.profiler_enable = 1
xdebug.profiler_output_name = xdebug.out.%t
xdebug.profiler_output_dir = /tmp
xdebug.profiler_enable_trigger = 1

xdebug.profiler_enable = 1
xdebug.profiler_output_name = xdebug.out.%t
xdebug.profiler_output_dir = /tmp
xdebug.profiler_enable_trigger = 1

Please note that profiling is disabled by default in Xdebug as it can reduce the performance of PHP. To enable it you will have to use xdebug.profiler_enable. You don’t want to run it all the time and thus make use of xdebug.profiler_enable_trigger to instruct Xdebug to perform profiling only when XDEBUG_PROFILE is passed as a GET or POST parameter.

请注意,Xdebug默认情况下禁用分析,因为它会降低PHP的性能。 要启用它,您将必须使用xdebug.profiler_enable。 您并不想一直运行它,因此只能使用xdebug.profiler_enable_trigger来指示Xdebug仅在将XDEBUG_PROFILE作为GET或POST参数传递时执行性能分析。

To sum up Xdebug is one of the most useful extensions for PHP developers. It controls the execution of PHP programs in the server. It also allows you a feature to remote debug and inspects values at runtime, without modifying your program. In the installation process we have discussed how to install Xdebub for remote debugging.

综上所述,Xdebug是PHP开发人员最有用的扩展之一。 它控制服务器中PHP程序的执行。 它还允许您使用功能进行远程调试并在运行时检查值,而无需修改程序。 在安装过程中,我们讨论了如何安装Xdebub进行远程调试。

翻译自: https://www.script-tutorials.com/php-debugging-and-profiling-xdebug/

php调试xdebug

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值