热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在VisualStudio代码中运行或调试php(VSCode)-HowtorunordebugphponVisualStudioCode(VSCode)

IcantfindawaytorunordebugphponVisualstudiocode,Doesanyoneknowhow?在Visualstudio代码

I can't find a way to run or debug php on Visual studio code, Does anyone know how?

在Visual studio代码中我找不到运行或调试php的方法,有人知道吗?

9 个解决方案

#1


13  

As far as i read about it today, you can't debug anything else than node.js, Javascript and TypeScript at the moment, but they said they want to add new languages which you can debug. The editor is still in development. Nevertheless, I don't think there will be a php debugger in the future since php is serverside, so you can't debug it on your client alone.

就我今天所读到的,除了节点,您不能调试其他任何东西。目前有js, Javascript和打字稿,但是他们说他们想添加新的语言,你可以调试。编辑仍在开发中。不过,我不认为将来会有php调试器,因为php是服务器端,所以您不能单独在客户机上调试它。

If you want to debug php, I can recommend xDebug.

如果您想调试php,我可以推荐xDebug。


Updated:

Now, it is possible to debug with VS code. You need to install XDebug and php-debug extension for VScode.

现在,可以使用VS代码进行调试。您需要为VScode安装XDebug和php-debug扩展。

#2


42  

Debugging PHP with VSCode using the vscode-php-debug extension

VSCode can now support debugging PHP projects through the marketplace extension vscode-php-debug.

VSCode现在支持通过marketplace扩展VSCode - PHP -debug调试PHP项目。

This extension uses XDebug in the background, and allows you to use breakpoints, watches, stack traces and the like:

这个扩展在后台使用XDebug,允许您使用断点、手表、堆栈跟踪等:

Screenshot: PHP Debugging in VSCode using vscode-php-debug extension

Installation is straightforward from within VSCode: Summon the command line with F1 and then type ext install php-debug

在VSCode中安装很简单:用F1调用命令行,然后输入ext install php-debug

#3


14  

There is now a handy guide for configuring PHP debugging in Visual Studio Code at http://blogs.msdn.com/b/nicktrog/archive/2016/02/11/configuring-visual-studio-code-for-php-development.aspx

现在有了一个方便的指南,可以在http://blogs.msdn.com/b/nicktrog/archive/2016/02/11/configurationvisualstudiocode -development.aspx中配置PHP调试。

From the link, the steps are:

从链接中,步骤如下:

  1. Download and install Visual Studio Code
  2. 下载并安装Visual Studio代码
  3. Configure PHP linting in user settings
  4. 在用户设置中配置PHP linting
  5. Download and install the PHP Debug extension from the Visual Studio Marketplace
  6. 从Visual Studio市场下载并安装PHP调试扩展
  7. Configure the PHP Debug extension for XDebug
  8. 为XDebug配置PHP调试扩展

Note there are specific details in the linked article, including the PHP values for your VS Code user config, and so on.

注意,在链接的文章中有一些特定的细节,包括VS代码用户配置的PHP值,等等。

#4


2  

It's worth noting that you must open project folder in Visual Studio Code for the debugger to work. I lost few hours to make it work while having only individual file opened in the editor.

值得注意的是,必须在Visual Studio代码中打开project文件夹,以便调试器工作。在编辑器中打开单个文件时,我花了几个小时让它工作。

Issue explained here

在这里所解释的问题

#5


1  

If you are using Ubuntu 16.04 and php7 you can install xdebug with below command:

如果您使用的是Ubuntu 16.04和php7,您可以使用以下命令安装xdebug:

sudo apt-get install php-xdebug

You can find the full configuration process here.

您可以在这里找到完整的配置过程。

If you are using windows, you can download xdebug from xdebug.org.

如果您正在使用windows,您可以从xdebug.org下载xdebug。

And start debugging in VS-code with php-debug extension.

并在vs代码中使用php-debug扩展开始调试。

how to configure php-debug for xdebug

#6


1  

To debug php with vscode,you need these things:

要使用vscode调试php,需要以下内容:

  1. vscode with php debuge plugin(XDebug) installed;
  2. 安装了php调试插件(XDebug)的vscode;
  3. php with XDebug.so/XDebug.dll downloaded and configured;
  4. php与XDebug.so / XDebug。dll下载和配置;
  5. a web server,such as apache/nginx or just nothing(use the php built-in server)
  6. 一个web服务器,比如apache/nginx或者什么都没有(使用php内置服务器)

you can gently walk through step 1 and 2,by following the vscode official guide.It is fully recommended to use XDebug installation wizard to verify your XDebug configuration.

通过遵循vscode官方指南,您可以轻松地完成第1步和第2步。建议使用XDebug安装向导来验证您的XDebug配置。

If you want to debug without a standalone web server,the php built-in maybe a choice.Start the built-in server by php -S localhost:port -t path/to/your/project command,setting your project dir as document root.You can refer to this post for more details.

如果您想在没有独立web服务器的情况下进行调试,可以选择内置的php。通过php -S localhost启动内置服务器:port -t路径/to/your/project命令,将项目dir设置为文档根。你可以参考这篇文章了解更多的细节。

#7


0  

already their is enough help full answers but if you want to see the process then
[ click here ]

他们的帮助已经足够完整的回答了,但是如果你想看到这个过程,那么点击这里

Steps in Short

步骤简而言之

  • download php debug plugin [ https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug ]
  • 下载php调试插件[https://marketplace.visualstudio.com/items?
  • download xDebug.dll [ https://xdebug.org/wizard.php ]
  • 下载xDebug。dll[https://xdebug.org/wizard.php]
  • move xdebug file to [ ?? / php / ext / here ]
  • 将xdebug文件移动到[??/ php / ext / here]
  • update php.ini file with following lines :

    更新php。ini文件,行如下:

    [XDebug] xdebug.remote_enable = 1 xdebug.remote_autostart = 1 zend_extension=path/to/xdebug

    XDebug XDebug。xdebug remote_enable = 1。remote_autostart = 1 / / xdebug zend_extension =路径

[ good to go ]

[很好]

  1. make sure that you have restarted your local server
  2. 确保重新启动本地服务器

source : https://www.youtube.com/watch?v=8MLEB1qx984

来源:https://www.youtube.com/watch?v=8MLEB1qx984

#8


0  

There is a much easier way to run PHP, no configuration needed:

运行PHP有一种更简单的方式,无需配置:

  1. Install the Code Runner Extension
  2. 安装代码运行器扩展
  3. Open the PHP code file in Text Editor
    • use shortcut Ctrl+Alt+N
    • 使用快捷键Ctrl + Alt + N
    • or press F1 and then select/type Run Code,
    • 或按F1,然后选择/键入运行代码,
    • or right click the Text Editor and then click Run Code in editor context menu
    • 或者右键单击文本编辑器,然后单击“编辑器上下文”菜单中的“运行代码”
    • or click Run Code button in editor title menu
    • 或者单击编辑器标题菜单中的Run Code按钮
    • or click Run Code button in context menu of file explorer
    • 或点击文件资源管理器上下文菜单中的“运行代码”按钮
  4. 在文本编辑器中打开PHP代码文件使用快捷键Ctrl + Alt + N或按F1然后选择/类型运行代码,或右键单击文本编辑器,然后单击运行代码在代码编辑器的上下文菜单或单击Run按钮编辑标题菜单或点击运行代码文件资源管理器的上下文菜单

Besides, you could select part of the PHP code and run the code snippet. Very convenient!

此外,还可以选择PHP代码的一部分并运行代码片段。非常方便!

#9


0  

It's actually possible to run PHP without xDebug and without installing any additional extensions.

实际上,在没有xDebug和不安装任何附加扩展的情况下运行PHP是可能的。

If you simply want to have the PHP executable evaluate PHP code and show output in the terminal (i.e. not in a browser), then you just need to copy the following configuration into your user settings:

如果您只是想让PHP可执行评估PHP代码并在终端(即不在浏览器中)显示输出,那么您只需将以下配置复制到您的用户设置中:

{
    "launch": {
        "version": "0.2.0",
        "configurations": [
            {
            "type": "php",
            "request": "launch",
            "name": "Run using PHP executable",
            "program": "${file}",
            "runtimeExecutable": "/usr/bin/php"
            },
        ]
    },
    // all your other user settings...
}

This creates a global launch configuration that you can use on any PHP file. Note the runtimeExecutable option. You will need to update this with the path to the PHP executable on your machine. After you copy the configuration above, whenever you have a PHP file open, you can press the F5 key to run the PHP code and have the output displayed in the vscode terminal.

这将创建一个全局启动配置,您可以在任何PHP文件上使用它。注意runtimeExecutable选项。您将需要使用机器上的PHP可执行文件的路径来更新它。复制上面的配置之后,无论何时打开PHP文件,都可以按F5键来运行PHP代码,并在vscode终端中显示输出。


On the other hand, if you want to run PHP by accessing a webserver with a browser, you will need to set this up by creating a build task. In order to create a task, you will need to be working on a workspace folder. Once you have a workspace folder open, go to Tasks > Configure Tasks... then press enter and then pick the "Others" option. Doing this will create a tasks.json file in your workspace folder.

另一方面,如果希望通过使用浏览器访问web服务器来运行PHP,则需要通过创建构建任务来设置它。为了创建一个任务,您需要在工作空间文件夹中工作。打开工作区文件夹后,转到Tasks >配置任务…然后按enter,然后选择“Others”选项。这样做会创建一个任务。工作空间文件夹中的json文件。

Now copy my configuration below into your tasks.json file:

现在将下面的配置复制到您的任务中。json文件:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Start Server",
            "type": "shell",
            "command": "php -S localhost:8080 -t ${workspaceFolder}",
            "isBackground": true,
            "group": "build",
            "problemMatcher": []
        },
        {
            "label": "Run In Browser",
            "type": "shell",
            "command": "open http://localhost:8080/${relativeFile}",
            "group": "build",
            "problemMatcher": []
        }
    ]
}

Now, if you have a PHP file from your workspace open, you can press Ctrl+Shift+B (⌘+Shift+B in MacOS), and the above two tasks should show up. Select Start Server to run PHP's built-in server, then Run In Browser to run the currently open file from your browser (this assumes your workspace root is also your document root). Note that if you already have a webserver running, you can remove the Start Server task and update the localhost:8080 part to point to whatever URL you are using.

现在,如果你有一个PHP文件从工作区中打开,你可以按Ctrl + Shift + B(MacOS⌘+ Shift + B),与上述两个任务应该出现了。选择Start Server来运行PHP的内置服务器,然后在浏览器中运行,从浏览器中运行当前打开的文件(假设工作区根也是文档根)。注意,如果已经有一个webserver在运行,那么可以删除Start Server任务并更新localhost:8080部分以指向正在使用的URL。

Another note: If you are using Windows, you will probably have to change open http://localhost:8080/${relativeFile} to something like explorer 'http://localhost:8080/${relativeFile}'

另一个注意事项:如果您正在使用Windows,您可能需要将打开的http://localhost:8080/${相对论文件}更改为类似于explorer的'http://localhost:8080/$ relativeFile}'


推荐阅读
author-avatar
fuzhipai
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有