1、先下载PHP扩展Xdebug https://xdebug.org, 可以复制自己的phpinfo粘贴到https://xdebug.org/wizard.php中, 会生成需要下载的版本。图文教程。
2、配置php.ini的设置语句.
PHP5.2\5.3 Xdebug 调试器配置及应用
PHP5.2添加的扩展方式:zend_extension_ts=D:\www\Server\php5\ext\php_xdebug-2.1.0-5.2-vc6.dll
PHP5.3添加的扩展方式:zend_extension=D:\www\Server\php5\ext\php_xdebug-2.1.0-5.3-vc6
注意:zend_extension\zend_extension_ts="扩展的绝对地址"
下载好(实际下载的是php_xdebug-2.0.0rc4-5.2.1.dll)之后放入php目录的ext文件夹中, 然后设置php.ini
[Zend]
; 这里必须关闭,否则与下面的xdebug冲突而导致后面无法启动
;zend_extension_ts="F:/phptools0/Zend/ZendExtensionManager.dll"
[Xdebug]
; 指定xdebug 扩展文件的位置(路径请根据自己的情况做调整,是绝对路径)
zend_extension_ts=F:/phptools0/PHP/ext/php_xdebug-2.0.0rc4-5.2.1.dll
; 启用xdebug 远程调试
xdebug.remote_enable = 1
; 这个与phpstorm--settings--servers--新建的那个一样
; 指定远程调试的主机名
xdebug.remote_host = localhost
; 这个与phpstorm--settings--debug--debug port设置成一样才行,尽量不要设置成9000
xdebug.remote_port = 9001
;下面这个开启远程调试自动启动-重要!不过开启后运行网页非常卡,所以还是设置为0较好,虽然下面两行重点强调其好处,但关闭后安装Chrome Xdebug插件也很方便
;是否自动连接客户端并调试。开启后,当用PHPSTORM调试时不需要先初始化连接XDEBUG服务端(绿虫子按钮),即不必安装Chrome Xdebug插件只要有断点运行网页即可自动debug
;只要在提交的字符串中包含XDEBUG_SESSION_START的参数并在代码行中设置了断点就会自动开始debug
xdebug.remote_autostart = 0
;设置用于开发服务器共享给多名开发者使用即remote_host是未指定的其他的也行
;表示获取请求发起地址(从 $_SERVER['HTTP_X_FORWARDED_FOR'] 和 $_SERVER['REMOTE_ADDR'] 中获取),反向访问发起地址的 9000 端口建立调试连接
;xdebug.remote_connect_back = 1
;xdebug.mode = 3
;xdebug.connect_timeout_ms = 10
; 【以下可不开启,主要是上面几行】
;xdebug.remote_mode = req
; 指定远程调试的处理协议
;xdebug.remote_handler = dbgp
; 指定传递给DBGp调试器处理程序的IDE Key,好像不设置没有任何问题
;xdebug.idekey = "phpstorm"
;是否收集变量
;xdebug.collect_vars = On
;是否收集返回值
;xdebug.collect_return = On
;是否收集参数
;xdebug.collect_params = On
;其他
xdebug.profiler_enable=off
xdebug.trace_output_dir="D:/phpzend/xdebug"
xdebug.profiler_output_dir="D:/phpzend/xdebug"
; 增加调试次数
;xdebug.max_nesting_level = 500
对于php7.3的xdebug调试,在php.ini文件最后直接加上如下(解释参照以下即可):
[Xdebug]
zend_extension=D:/phpstudy_pro/Extensions/php/php7.3.4nts/ext/php_xdebug.dll
xdebug.remote_enable = 1
xdebug.remote_host = localhost
xdebug.remote_port = 9001
xdebug.remote_autostart = 0
;xdebug.collect_params=1
;xdebug.collect_return=1
;xdebug.auto_trace=Off
;xdebug.trace_output_dir=D:/phpstudy_pro/Extensions/php_log/php5.6.9nts.xdebug.trace
;xdebug.profiler_enable=Off
;xdebug.profiler_output_dir=D:/phpstudy_pro/Extensions/php_log/php5.6.9nts.xdebug.profiler
;xdebug.remote_enable=Off
;xdebug.remote_host=localhost
;xdebug.remote_port=9001
;xdebug.remote_handler=dbgp
3、检查配置是否成功
在命令行中输入如下观察结果有xdebug表示成功
php -version
php -m
4、设置phpstorm的调试
(1)设置端口phpstorm--settings--debug--debug port为上述php.ini一样的端口9001
(2)设置服务器phpstorm--settings--debug--servers,新建一个,Host与php.ini对应比如是localhost,此时端口设置成apache/nginx一样的,比如80
(3)设置调试选项,phpstorm--run--edit configrations--新建php web pagep--有个配置设置成上述servers的比如是localhost,关于DBGp Proxy里空着,没设置,也可以,如果设置则应该与上面配置文件中的一致。
5、初级调试
设置断点,开启 debug 监听,点击绿色的甲壳虫进行开始调试,这个小电话一定的是接通的状态,才可以的。此时可不用打开浏览器。正常到这里就行了(phpstorm中调试选择current file),不必进行以后步骤的。
6、安装Chrome的 Xdebug Helper,使用办法,在phpstorm设置断点,点击浏览器Debug选项之后,在网页运行页面即可在phpstorm中显示变量。
7、如果是vscode则如下(php.ini直接使用上面的设置,不必更改什么):
(1)vscode中安装PHP Debug(名字是Xdebug且是蓝色对勾)。
(2)vscode中配置settings.json
"php.debug.executablePath": "F:\\phptools0\\PHP\\php.exe",
(3)vscode中运行调试时会生成这个文件launch.json,生成时有个下拉选择php(不要选择php xdebug),注意修改其中的端口与php.ini中一致。
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9001
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 0,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "Launch Built-in web server",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:0"
],
"program": "",
"cwd": "${workspaceRoot}",
"port": 9001,
"serverReadyAction": {
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
}
}
]
}
(3)在vscode中的test.php中设置断点,并调试,然后在网页中打开此网页链接,注意网页中xdebu helper中开启debug模式,此时就能在vscode中观察到断点变量。
8、其他教程:链接地址。