准备工作与环境介绍
准备必要的软件安装包是搭建调试环境的第一步。
首先获取 phpStudy 和 VS Code 的安装程序。phpStudy 下载地址为 https://www.xp.cn/download.html ,VS Code 官方下载链接为 https://code.visualstudio.com/ 。本教程基于 phpStudy 版本 7.3.4nts 进行演示。

[Xdebug]
zend_extension=D:/phpstudy_pro/Extensions/php/php7.3.4nts/ext/php_xdebug.dll
xdebug.collect_params=1
xdebug.collect_return=1
xdebug.auto_trace=On
xdebug.trace_output_dir=D:/phpstudy_pro/Extensions/php_log/php7.3.4nts.xdebug.trace
xdebug.profiler_enable=On
xdebug.profiler_output_dir=D:/phpstudy_pro/Extensions/php_log/php7.3.4nts.xdebug.profiler
xdebug.remote_enable=On
xdebug.remote_autostart=On
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
打开 VS Code 编辑器,通过快捷键 Ctrl+逗号 调出设置界面,或者依次点击菜单栏的文件 -> 首选项 -> 设置进入配置页面。

VS Code PHP 路径配置
在 VS Code 的设置中,需要指定 PHP 可执行文件的路径,以便编辑器能正确识别 PHP 环境。具体配置如下: [[a3]] 其中关键配置项 "php.validate.executablePath" 指向了 phpStudy 安装的 php.exe 路径:D:/phpstudy_pro/Extensions/php/php7.3.4nts/php.exe。
{
"window.zoomLevel": 1,
"git.confirmSync": false,
"git.autofetch": true,
"[javascript]": {
"editor.defaultFormatter": "vscode.typescript-language-features"
},
//关键点
"php.validate.executablePath": "D:/phpstudy_pro/Extensions/php/php7.3.4nts/php.exe"
}
Xdebug 扩展参数设置
接下来需要在 phpStudy 中配置 Xdebug 扩展。找到 phpStudy 对应的 php.ini 配置文件,添加或修改以下 Xdebug 相关参数: [[a1]] 主要配置说明: - zend_extension 指向 php_xdebug.dll 所在目录。 - xdebug.remote_enable 和 xdebug.remote_autostart 设为 On 以启用远程调试并自动启动。 - xdebug.remote_host 设为 localhost,xdebug.remote_port 设为 9000,确保调试端口一致。 - 其他如 trace 和 profiler 输出目录可根据需要调整。
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
//端口
"port": 9000
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
//端口
"port": 9000
}
]
}
完成上述配置后,重启 phpStudy 服务以使 Xdebug 配置生效。可以通过访问 phpinfo() 页面检查 Xdebug 模块是否已加载成功。

VS Code 调试器配置与验证
回到 VS Code,在项目根目录下创建 .vscode/launch.json 文件(或通过 F5 启动调试时自动生成),配置调试器连接信息: [[a4]] 这里定义了两个配置项: 1. Listen for XDebug:监听来自浏览器的 Xdebug 连接,端口为 9000。 2. Launch currently open script:直接运行当前打开的脚本文件,同样使用端口 9000。 通常选择第一个配置进行浏览器端调试。
在浏览器中访问测试页面,例如输入地址 http://localhost/test.php ,触发代码执行。

此时在 VS Code 中按下 F5 键启动调试模式,若配置正确,当代码执行到断点处时,VS Code 将暂停执行并进入调试状态,可查看变量、调用堆栈等信息,从而完成整个调试环境的搭建。