如果您不想安装 xDebug 或其他扩展,而只想运行 PHP 文件而不进行调试,您可以使用构建任务来完成此操作。
使用构建任务
首先打开命令面板(Ctrl+Shift+P在Windows中,⌘+ Shift+P in Mac),然后选择“Tasks:Open User Tasks”。现在将我的配置复制到您的 tasks.json 文件中。这将创建可以在任何时间和任何工作空间中使用的用户级任务。
{
"version": "2.0.0",
"tasks": [
{
"label": "Start Server",
"type": "shell",
"command": "php -S localhost:8080 -t ${fileDirname}",
"isBackground": true,
"group": "build",
"problemMatcher": []
},
{
"label": "Run In Browser",
"type": "shell",
"command": "open http://localhost:8080/${fileBasename}",
"windows": {
"command": "explorer 'http://localhost:8080/${fileBasename}'"
},
"group": "build",
"problemMatcher": []
}
{
"label": "Run In Terminal",
"type": "shell",
"command": "php ${file}",
"group": "none",
"problemMatcher": []
}
]
}
如果您想在终端中运行 php 文件,请打开命令面板并选择“任务:运行任务”,然后选择“在终端中运行”。
如果您想在响应网络浏览器的网络服务器上运行代码,请打开命令面板并选择“任务:运行任务”,然后选择“启动服务器”以运行 PHP 的内置服务器,然后“在浏览器中运行”从浏览器运行当前打开的文件。
请注意,如果您已经有一个网络服务器正在运行,您可以删除 Start Server 任务并更新 localhost:8080 部分以指向您正在使用的任何 URL。
使用 PHP 调试
注意:这部分是在我原来的答案中。我最初认为它可以在没有 PHP Debug 的情况下工作,但看起来 PHP Debug 实际上在启动配置中公开了 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...
}
这将创建一个全局启动配置,您可以在任何 PHP 文件上使用它。注意runtimeExecutable 选项。您需要使用机器上 PHP 可执行文件的路径来更新它。复制上述配置后,只要打开 PHP 文件,就可以按 F5 键运行 PHP 代码,并在 vscode 终端中显示输出。