VSCode和PhpStorm配置进行PHP断点调试

no alter data available

VSCode和PhpStorm配置进行PHP断点调试

2024-04-28 16:12

本文环境:php版本:5.4.45 (phpstudy),Xdebug 2.4.1(phpstudy文件夹中自带),phpstorm 2021.3

TOC

为啥是这么老的版本呢?😂 因为在改老项目,其它版本应该都是类似的。

PHP配置

要在php的配置文件中指定Xdebug的路径,和它的一些配置,具体在php.ini文件中,配置以下内容(路径要对应修改):

[XDebug]
xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug"
xdebug.trace_output_dir="D:\phpStudy\tmp\xdebug"
zend_extension="D:\phpStudy\php\php-5.4.45\ext\php_xdebug.dll"
xdebug.profiler_enable=on
xdebug.auto_trace=on
xdebug.collect_params=on
xdebug.collect_return=on
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.idekey=PHPSTORM

关于Xdebug

  • 官网:https://xdebug.org/
  • 若是不知道要下哪个版本的Xdebug的话,可以将phpinfo(); 输出的信息,放在下面这个地址: https://xdebug.org/wizardimage 它会自动帮你分析,当前php所对应的版本,让你下载。当然还有一种可能,它会说你当前的版本不支持,可以选择相近的版本尝试下。

浏览器配置(插件安装)

  1. 需要安装Xdebug helper插件,商店地址:https://chrome.google.com/webstore/detail/xdebug-helper/eadndfjplgieldjbigjakmdgkmoaaaoc 无法下载则可使用我上传到网盘的:https://cloud.189.cn/t/6zyMnajyamaq (访问码:is3y)

image

  1. 安装好后右键进入配置,也是设置idekey,就是php.ini对应的。 image
  2. 开启调试 image

使用PHPStrom配置

  1. 在设置中指定Xdebug的端口相关,就是上面php.ini中配置的: image
  2. 这里要开启对debug的监听: image

使用VSCode配置

    开始是使用VScode开发的,找了挺多教程都断点不了,而且公司电脑装PHPStom要申请,然后在自己电脑上装了PHPStorm下来,然后再看了下拓展的文档,VScode也能断点了。     其实主要就是之前的浏览器插件没装(看了挺多教程都没提到😂)

PHP Debug Adapter for Visual Studio Code(文档): https://github.com/xdebug/vscode-php-debug/blob/main/README.md

  1. 在完成了之前的php配置,和浏览器配置后。要在VSCode中断点调试PHP的话要装一个插件PHP Debugimage
  2. 然后进行了调试菜单,点击运行调试,或者点运行边上的小齿轮图标,可以看到调试运行配置的JSON,修改其端口号与php.ini中相同,保存后就可以进行调试了。
点击查看代码
{
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": 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": 9000,
      "serverReadyAction": {
        "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
        "uriFormat": "http://localhost:%s",
        "action": "openExternally"
      }
    }
  ]
}

断点调试

到此一切准备就绪,打上断点就可以在愉快的调试了。

  • PHPSTORM: image
  • VSCode: image