如何在 Visual Studio Code (VSCode) 上运行或调试 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29960999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run or debug php on Visual Studio Code (VSCode)
提问by omer2802
I can't find a way to run or debug php on Visual studio code, Does anyone know how?
我找不到在 Visual Studio 代码上运行或调试 php 的方法,有谁知道如何?
采纳答案by Realit?tsverlust
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.
就我今天读到的内容而言,目前除了 node.js、JavaScript 和 TypeScript 之外,您无法调试任何其他内容,但他们说他们想添加可以调试的新语言。编辑器仍在开发中。尽管如此,我认为未来不会有 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 XDebugand php-debugextension for VScode.
回答by flexponsive
Debugging PHP with VSCode using the vscode-php-debugextension
使用vscode-php-debug扩展使用VSCode调试 PHP
VSCode can now support debugging PHP projects through the marketplace extension vscode-php-debug.
VSCode 现在可以通过市场扩展vscode-php-debug支持调试 PHP 项目。
This extension uses XDebug in the background, and allows you to use breakpoints, watches, stack traces and the like:
此扩展在后台使用 XDebug,并允许您使用断点、监视、堆栈跟踪等:
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
回答by John Kaster
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/configuring-visual-studio-code-for-php-development 中有一个在 Visual Studio Code 中配置 PHP 调试的方便指南.aspx
From the link, the steps are:
从链接中,步骤是:
- Download and install Visual Studio Code
- Configure PHP linting in user settings
- Download and install the PHP Debug extension from the Visual Studio Marketplace
- Configure the PHP Debug extension for XDebug
- 下载并安装 Visual Studio Code
- 在用户设置中配置 PHP linting
- 从 Visual Studio Marketplace 下载并安装 PHP 调试扩展
- 为 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 Code 用户配置的 PHP 值等。
回答by Kodos Johnson
It's actually possible to runPHP 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.
另一方面,如果您想通过使用浏览器访问网络服务器来运行 PHP,则需要通过创建构建任务来进行设置。为了创建任务,您需要处理工作区文件夹。打开工作区文件夹后,转到Tasks > Configure Tasks...
然后按回车键,然后选择“其他”选项。这样做将在您的工作区文件夹中创建一个 tasks.json 文件。
Now copy my configuration below into your tasks.json file:
现在将我下面的配置复制到您的 tasks.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}",
"windows": {
"command": "explorer '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 的内置服务器,然后Run In Browser
从浏览器运行当前打开的文件(假设您的工作区根也是您的文档根)。请注意,如果您已经有一个网络服务器在运行,您可以删除该Start Server
任务并更新该localhost:8080
部分以指向您正在使用的任何 URL。
回答by Jun Han
There is a much easier way to run PHP, no configuration needed:
有一种更简单的方式来运行 PHP,无需配置:
- Install the Code Runner Extension
- Open the PHP code file in Text Editor
- use shortcut
Ctrl+Alt+N
- or press
F1
and then select/typeRun Code
, - or right click the Text Editor and then click
Run Code
in editor context menu - or click
Run Code
button in editor title menu - or click
Run Code
button in context menu of file explorer
- use shortcut
- 安装代码运行器扩展
- 在文本编辑器中打开 PHP 代码文件
- 使用快捷方式
Ctrl+Alt+N
- 或按
F1
然后选择/键入Run Code
, - 或右键单击文本编辑器,然后
Run Code
在编辑器上下文菜单中单击 - 或单击
Run Code
编辑器标题菜单中的按钮 - 或单击
Run Code
文件资源管理器上下文菜单中的按钮
- 使用快捷方式
Besides, you could select part of the PHP code and run the code snippet. Very convenient!
此外,您可以选择部分 PHP 代码并运行代码片段。很方便!
回答by insCode
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]
- download xDebug.dll [ https://xdebug.org/wizard.php]
- move xdebug file to [ ?? / php / ext / here ]
update php.ini file with following lines :
[XDebug] xdebug.remote_enable = 1 xdebug.remote_autostart = 1 zend_extension=path/to/xdebug
- 下载 php 调试插件 [ https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug]
- 下载 xDebug.dll [ https://xdebug.org/wizard.php]
- 将 xdebug 文件移动到 [ ?? / php / ext / 这里]
使用以下几行更新 php.ini 文件:
[XDebug] xdebug.remote_enable = 1 xdebug.remote_autostart = 1 zend_extension=path/to/xdebug
[ good to go ]
[好去]
- make sure that you have restarted your local server
- 确保您已重新启动本地服务器
回答by emil f.
回答by Archman
To debug php with vscode,you need these things:
要使用 vscode 调试 php,你需要这些东西:
- vscode with php debuge plugin(XDebug) installed;
- php with XDebug.so/XDebug.dll downloaded and configured;
- a web server,such as apache/nginx or just nothing(use the php built-in server)
- 安装了 php 调试插件(XDebug)的 vscode;
- 下载并配置了 XDebug.so/XDebug.dll 的 php;
- 一个 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 wizardto 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 postfor more details.
如果你想在没有独立web服务器的情况下调试,那么内置的php可能是一个选择。通过php -S localhost:port -t path/to/your/project
命令启动内置服务器,将你的项目目录设置为文档根目录。更多细节可以参考这篇文章。
回答by shibli049
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-debugextension.
并使用php-debug扩展在 VS-code 中开始调试。
回答by Rain
The best solution for me was to add a key binding to run PHP code directly in the terminal
对我来说最好的解决方案是添加一个键绑定来直接在终端中运行 PHP 代码
To do so you just need to download terminal-command-keys
from VS code extensions marketplace:
为此,您只需terminal-command-keys
要从 VS 代码扩展市场下载:
Then got to File>Preferences>Keyboard Shortcutsand click on the following icon at the upper right corner:
然后转到文件>首选项>键盘快捷方式并单击右上角的以下图标:
It will open up the keybindings.json
file
它会打开keybindings.json
文件
Add the following settings
添加以下设置
[
{
"key": "ctrl+s",
"command":"terminalCommandKeys.run",
"when": "editorLangId == php",
"args": {
"cmd":"php ${file}",
"newTerminal":true,
"saveAllfiles": true,
"showTerminal": true,
}
}
]
keyis the shortcut to run your PHP file (I use ctrl+s) you can change it as you wish
key是运行 PHP 文件的快捷方式(我使用 ctrl+s),您可以随意更改它
whento run different commands for different file types (I set it for PHP files only) vscode's "when" clauses
何时为不同的文件类型运行不同的命令(我只为 PHP 文件设置)vscode 的“when”子句
See the full settings documentation from here
从这里查看完整的设置文档
That's it, I hope it helps.
就是这样,我希望它有帮助。