VS Code 默认不识别 PHP 语法高亮,需安装 PHP 扩展(如 Intelephense)、手动设置语言模式为 PHP,并配置 "files.associations": {"*.php": "php"};主题需支持 PHP token scope,微调须用 editor.tokenColorCustomizations 嵌套 languages.php。
VS Code 默认不识别 PHP 语法高亮,必须先装对扩展、配好语言模式,否则调颜色主题纯属白忙。
很多人改了主题却没效果,根本原因是 VS Code 没把当前文件识别为 PHP。它不会自动根据 .php 后缀判断——你得手动触发或配置默认关联。
.php 文件后,看窗口右下角状态栏,点击语言模式(通常显示 Plain Text 或 PHP)→ 选 PHP
Ctrl+Shift+P(Windows/Linux)或 C
md+Shift+P(macOS),输入 Preferences: Configure Language Specific Settings... → 选 PHP → 在弹出的 settings.json 片段里加:"files.associations": {
"*.php": "php"
}PHP Intelephense 或 PHP Server 类扩展;仅装主题扩展无法激活语法解析直接改 workbench.colorCustomizations 只能调界面色(如侧边栏、标题栏),PHP 代码高亮靠的是 editor.tokenColorCustomizations,且依赖当前主题是否导出 token scope。
One Dark Pro、Nord、Dracula Official,它们都预置了完整的 PHP token 映射Ctrl+,)→ 搜 token color customizations → 点击 Edit in settings.json → 添加"editor.tokenColorCustomizations": {
"textMateRules": [
{
"scope": "support.class.php",
"settings": {
"foreground": "#56B6C2"
}
},
{
"scope": "keyword.control.php",
"settings": {
"fontStyle": "bold"
}
}
]
}Ctrl+Shift+P → Developer: Inspect Editor Tokens and Scopes 点击代码任意位置实时查看真实 scope在 editor.tokenColorCustomizations 里直接写规则,会影响所有语言。PHP 特定样式必须嵌套进 languages 字段,否则 Vue/JSX 里的 片段也会被误染色。
"editor.tokenColorCustomizations": {
"languages": {
"php": {
"textMateRules": [
{
"scope": "variable.other.php",
"settings": {
"foreground": "#E06C75"
}
}
]
}
}
}string、comment 这类通用 scope 在 PHP 里实际对应更细的 string.quoted.double.php 或 comment.block.php,直接写 string 会拉垮整个编辑器的字符串颜色Ctrl+Shift+P → Developer: Reload Window),热更新不生效真正卡住人的从来不是“怎么换颜色”,而是 PHP token scope 的嵌套层级和语言模式是否就位——少一步,改半天也看不到变量变红。