博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
为 PhpStorm 配置 Xdebug 来调试代码
阅读量:4352 次
发布时间:2019-06-07

本文共 2922 字,大约阅读时间需要 9 分钟。

当项目越来越复杂,排错就越发困难。

你以为代码是这么运行的,但就是有未想到的功能导致流程变得不可捉摸。
此时我们需要调试啊调试...

是一款优秀的 PHP IDE,排除其 Java 系出身导致的资源占用情况不理想外,其功能和易用性是毋庸质疑的。

好,再说下去就是软文了。

PhpStorm 内建了 Zend Debugger 和 Xdebug 支持,使用简单的配置我们就可以开始调试代码了。我们以 Xdebug 为例来说明下。

为 PHP 安装 Xdebug 扩展

Win 下的安装较为简单,直接下载适合的版本的 dll 文件,写入到 php.ini 配置中就可以了。

我们介绍下源代码编译方式。
在 找到最新的源代码,现在的版本是 :

cd
/Users
/micate
/Downloads
/
wget http:
//xdebug.org
/files
/xdebug-2.2.1.tgz
tar zxvf xdebug-2.2.1.tgz
cd xdebug-2.2.1
/
 
phpize
.
/configure;
make
sudo
make
install

嗯,不出意外的话,会提示 xdebug.so 已经被放置在 ... 位置上。记住这个位置,复制下来。

打开 php.ini,在最后面追加下述配置:

[xdebug]
zend_extension
=
"/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
 
xdebug.idekey
=
"PHPSTORM"
 
xdebug.remote_host
=
127.0.0.1
 
xdebug.remote_enable
=
on

上面的位置要与刚刚安装的位置保持一致。关于 xdebug.so 如何配置,:

Configure PHP to Use Xdebug

  1. add the following line to php.ini: zend_extension="/wherever/you/put/it/xdebug.so" (for non-threaded use of PHP, for example the CLI, CGI or Apache 1.3 module) or: zend_extension_ts="/wherever/you/put/it/xdebug.so" (for threaded usage of PHP, for example the Apache 2 work MPM or the the ISAPI module). Note: In case you compiled PHP yourself and used --enable-debug you would have to use zend_extension_debug=. From PHP 5.3 onwards, you always need to use the zend_extension PHP.ini setting name, and not zend_extension_ts, nor zend_extension_debug. However, your compile options (ZTS/normal build; debug/non-debug) still need to match with what PHP is using.
  2. Restart your webserver.
  3. Write a PHP page that calls 'phpinfo()' Load it in a browser and look for the info on the Xdebug module. If you see it next to the Zend logo, you have been successful! You can also use 'php -m' if you have a command line version of PHP, it lists all loaded modules. Xdebug should appear twice there (once under 'PHP Modules' and once under 'Zend Modules').

其中使用 zend_extension_ts 还是 zend_extension 还是神马神马,与 PHP 版本有关,仔细看上面的说明。

重启下 Web Server(Apache / Nginx),使用 phpinfo() 页面或命令行工具,确认模块安装完成。

配置 PhpStorm

本节参考了 一文:

PhpStorm 以最新版的 4.0.3 为例,版本不同操作会有些许区别。

打开 PhpStorm,新建或打开一个项目,打开偏好设置界面:

依图所示,确认 Debug 设置无误(提示,可以在左上角直接搜索 Debug 来定位到设置项):

单击左侧的 Servers,添加一个 Debug Server:

其中 Host 和 Port 根据自己要调试的实际情况来写,先不要加目录或查询字符串。后面选择 Xdebug。
然后 Apply 和 OK,回到编辑器界面。

点击菜单里的 Run - Configurations:

在 运行/调试 对话框中,依图配置:

如图所示:点击 添加 按钮,选择 PHP Web Application,并在右侧输入配置名称,选择我们刚刚添加的 Server,Start URL 中填写调试可能所需的查询字符串,选择好默认浏览器,最后点击 Apply 和 OK,确认配置。

然后,还有一步:

  1. 在程序中设置好断点;
  2. 在工具栏中,选择好要调试的应用;
  3. 点击 Start Listen PHP Debug Connections 按钮,即那个红色的电话按钮,让其变成绿色,即开始监听 PHP Debug 连接;

于是,终于可以开始了。

点击上图中的 Debug 按钮,或者从菜单中选择 Run - Debug,你就开始了 Debug 之旅...

其他说明

根据断点配置,或在打开 Debug URL 的过程中,或在 POST 之后,如果 PhpStorm 监听到了 Debug 连接,就会立即切换到编辑器界面,并跳转到设置的断点处,浏览器端会等待 PhpStorm 的操作。

你可以随时中断 PhpStorm 的调试,或方便的进行 Step Into / Step Over / Run to cursor(这个刁爆了):

熟悉 Java 的童鞋会对这个界面感到亲切,吼吼。

哎呀,不想调试了,PhpStorm 却总是跳出来?记得刚刚那个电话按钮嘛,再点一下,让其变成红色,就好了。

先到这里,嗯哼。自己捣鼓捣鼓吧。

来源: <>
 

转载于:https://www.cnblogs.com/holyes/p/3241170.html

你可能感兴趣的文章
静态链接与动态链接的区别
查看>>
如何使用mysql
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-05 微服务调用方式之feign 实战 订单调用商品服务...
查看>>
技术分析淘宝的超卖宝贝
查看>>
Azure云服务托管恶意软件
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
类中的静态函数和非静态函数的区别
查看>>
windows 下安装Apache
查看>>