当项目愈来愈复杂,排错就愈加困难。
你觉得代码是这么运行的,但就是有未想到的功能致使流程变得不可捉摸。
此时咱们须要调试啊调试...javascript
PhpStorm 是一款优秀的 PHP IDE,排除其 Java 系出身致使的资源占用状况不理想外,其功能和易用性是毋庸质疑的。
好,再说下去就是软文了。php
PhpStorm 内建了 Zend Debugger 和 Xdebug 支持,使用简单的配置咱们就能够开始调试代码了。咱们以 Xdebug 为例来讲明下。java
Win 下的安装较为简单,直接下载适合的版本的 dll 文件,写入到 php.ini 配置中就能够了。
咱们介绍下源代码编译方式。
在 Xdebug 下载页面 找到最新的源代码,如今的版本是 2.2.1:web
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,在最后面追加下述配置:app
[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 如何配置,Xdebug 文档中有说明:phpstorm
Configure PHP to Use Xdebug编辑器
- 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.
- Restart your webserver.
- 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 版本有关,仔细看上面的说明。ide
重启下 Web Server(Apache / Nginx),使用 phpinfo() 页面或命令行工具,确认模块安装完成。工具
本节参考了 Configuring PHP debugging in PhpStorm 2.0 一文:
PhpStorm 以最新版的 4.0.3 为例,版本不一样操做会有些许区别。
打开 PhpStorm,新建或打开一个项目,打开偏好设置界面:
依图所示,确认 Debug 设置无误(提示,能够在左上角直接搜索 Debug 来定位到设置项):
单击左侧的 Servers,添加一个 Debug Server:
其中 Host 和 Port 根据本身要调试的实际状况来写,先不要加目录或查询字符串。后面选择 Xdebug。
而后 Apply 和 OK,回到编辑器界面。
在 运行/调试 对话框中,依图配置:
如图所示:点击 添加 按钮,选择 PHP Web Application,并在右侧输入配置名称,选择咱们刚刚添加的 Server,Start URL 中填写调试可能所需的查询字符串,选择好默认浏览器,最后点击 Apply 和 OK,确认配置。
因而,终于能够开始了。
点击上图中的 Debug 按钮,或者从菜单中选择 Run - Debug,你就开始了 Debug 之旅...
根据断点配置,或在打开 Debug URL 的过程当中,或在 POST 以后,若是 PhpStorm 监听到了 Debug 链接,就会当即切换到编辑器界面,并跳转到设置的断点处,浏览器端会等待 PhpStorm 的操做。
你能够随时中断 PhpStorm 的调试,或方便的进行 Step Into / Step Over / Run to cursor(这个刁爆了):
熟悉 Java 的童鞋会对这个界面感到亲切,吼吼。
哎呀,不想调试了,PhpStorm 却老是跳出来?记得刚刚那个电话按钮嘛,再点一下,让其变成红色,就行了。
先到这里,嗯哼。本身捣鼓捣鼓吧。