×
新网 > 域名资讯 > 正文

PHP-Xdebug在PhpStorm中的使用

我们在软件开发的时候,经常要进行调试,其中有一种调试方法就是断点,此时可以用PHP的Xdebug扩展来使用。

我们在软件开发的时候,经常要进行调试,其中有一种调试方法就是断点,此时可以用PHP的Xdebug扩展来使用。

1 (30).jpg

1, 安装PHP-Xdebug扩展;

sudo apt-get install php-xdebug

安装完成后,用 php -v的命令查看结果

php -v PHP 5.6.30-12~ubuntu16.04.1+deb.sury.org+1 (cli) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans

可以看到已经有Xdebug v2.5.5安装好了.

2, 在php的配置文件中对xdebug扩展进行配置,这里有两种方式:

a: 最直接的方式,是找到php.ini,在最后面加入以下配置项;

sudo vim /etc/php/5.6/fpm/php.ini

zend_extension=/usr/lib/php/20131226/xdebug.so xdebug.remote_host=localhost xdebug.remote_port=9100 xdebug.remote_enable=1 xdebug.remote_autostart=1

b: 另一种是在Xdebug的配置文件中,加入相同的配置项;

我们可以在/etc/php/5.6/fpm/conf.d中看到

20-xdebug.ini -> /etc/php/5.6/mods-available/xdebug.ini

可以进入这个文件中,看到这里已经讲zend_extension的路径添加进去了,将其他配置添加进去

zend_extension=xdebug.so

 

以上两种方法是等价的,因为php的配置文件会引入各个扩展的配置文件

 

我们逐项来看看这些配置分别代表什么意思

① xdebug.remote_host=localhost

Type:string, Default value:localhost

Selects the host where the debug client is running, you can either use a host name, IP address, or \'unix:///path/to/sock\' for a Unix domain socket. This setting is ignored ifxdebug.remote_connect_backis enabled.

Support for Unix domain sockets was introduced in Xdebug 2.6.

选择debug客户端在那个主机上运行,可以设置成一个host名称,IP地址,或者Unix域名的路径. 如果xdebug.remote_connect_back被设置为 enabled时,此host设置将被忽略。

 

② xdebug.remote_port=9100

Type:integer, Default value:9000

The port to which Xdebug tries to connect on the remote host. Port 9000 is the default for both the client and the bundled debugclient. As many clients use this port number, it is best to leave this setting unchanged.

这是Xdebug尝试连接的远程host的端口. 9000端口是客户端和随附的debug客户端的默认端口。由于很多客户端都使用这个端口号,这个配置一旦设好,最好保持不变。

 

③xdebug.remote_enable=1
Type: boolean, Default value: 0
This switch controls whether Xdebug should try to contact a debug client which is listening on the host and port as set with the settings xdebug.remote_host and xdebug.remote_port. If a connection can not be established the script will just continue as if this setting was 0.

此开关控制着Xdebug是否应该尝试联系debug客户端,这个客户端正在监听配置好的host和port. 如果这项配置为0,则当无法连接时,脚本会啥都不做,继续执行。

 

④ xdebug.remote_autostart=1

Type:boolean, Default value:0

Normally you need to use a specific HTTP GET/POST variable to start remote debugging (seeRemote Debugging). When this setting is set to 1, Xdebug will always attempt to start a remote debugging session and try to connect to a client, even if the GET/POST/COOKIE variable was not present.

通常你需要使用一个特定的HTTP GET/POST 变量来启动远程bugging. 当此配置为1时, Xdebug将永远会试图远程debugging session,并尝试连接一个客户端,即便 GET/POST/COOKIE变量没有出现。

 

3, 重启php服务

sudo service php5.6-fpm restart

 

4, 在IDE中添加Debug的Host和Port,进入PhpStorm->File->Settings->Languages & Frameworks->Servers->+

保存后,此时可以在具体的代码文件页面看到debug的标识,

在这里单击一下,有一个红色的圆圈,代表在此处设置一个断点(break point)

用Postman请求到这个文件,可以看到在IDE下方,出现了很多信息

这些变量值直接展示出来了,而不要再另外去打断点来查看。

 

当然,以上只是xdebug很小的一部分功能。更重要的是用来做性能分析,代码逻辑优化等,这个过一段时间再来更新。

  • 相关专题

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣

Loading