You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio Code中PHP保存自动格式化:Beautify插件能否实现及配置

在VS Code中实现PHP文件保存自动格式化的方案

Hey there, let's walk through how to get auto-formatting working for your PHP files in VS Code—covering whether Beautify can do the job, how to configure it, and a solid alternative if it's not quite what you need.

Can the Beautify plugin handle this?

Absolutely! The Beautify plugin (by HookyQR) supports PHP formatting, and you can easily set it up to run automatically when you save a file.

Step-by-step configuration for Beautify

  1. First, head to the VS Code Extensions Marketplace (click the square icon on the left sidebar), search for Beautify, and install the one by HookyQR.
  2. Open your VS Code settings: Use the shortcut Ctrl+, (Windows/Linux) or Cmd+, (Mac), or click the gear icon in the bottom-left corner and select "Settings".
  3. Turn on the global "format on save" toggle: Search for "Format On Save" in the settings search bar, then check the box next to Editor: Format On Save.
  4. Set Beautify as the default formatter for PHP files:
    • Either search for "Default Formatter" in settings, select HookyQR.beautify as the global default, or set it specifically for PHP to avoid affecting other file types.
    • For PHP-specific setup, switch to the JSON view of settings (click the curly braces {} icon in the top-right of the settings page) and add this code:
      "[php]": {
          "editor.defaultFormatter": "HookyQR.beautify",
          "editor.formatOnSave": true
      }
      
  5. (Optional) Customize formatting rules: If you want to tweak how your PHP code is formatted (like indent size or line wrap length), create a .jsbeautifyrc file in your project's root directory with settings like this:
    {
        "php": {
            "indent_size": 4,
            "indent_char": " ",
            "wrap_line_length": 120,
            "space_after_cast": true
        }
    }
    

Alternative plugin if Beautify isn't working for you

If you find Beautify's PHP formatting doesn't match your preferences, PHP Intelephense is a fantastic alternative. It's not just a formatter—it also offers top-notch code completion, syntax checking, and refactoring tools for PHP.

To set up auto-formatting with PHP Intelephense:

  1. Install the plugin from the Extensions Marketplace.
  2. Update your settings JSON with this configuration:
    "[php]": {
        "editor.defaultFormatter": "bmewburn.vscode-intelephense-client",
        "editor.formatOnSave": true
    }
    
  3. You can adjust its formatting rules via the settings panel (search for "Intelephense" to find options like indent size, brace style, etc.).

内容的提问来源于stack exchange,提问作者codeDragon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:39:41