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}'