Update 2021-07-21

It's been more than half a decade since I first wrote this answer. The extensions to which I originally linked are abandoned, and Visual Studio Code's intrinsic PHP support hasn't improved, which is disappointing. The only decent extension still standing of which I'm aware is PHP Intelephense, which uses a freemium model: basic features are free, and a lifetime license is $12 USD as of writing.

The free version of Intelephense supports code formatting with the usual shortcuts (Alt + Shift + F on Windows and Linux, ⌥⇧F on macOS). Visual Studio Code continues to lack built-in support for PHP code formatting and will direct you to the extension marketplace if you attempt to format PHP without an appropriate extension installed.

Original answer

Visual Studio Code has pretty awesome PHP support. What it lacks is covered by extensions. A quick search reveals at least three (1, 2, and 3) that claim to support PHP formatting.

They mostly seem to use the standard shortcut of Alt + Shift + F on Windows/Linux, with varying shortcuts on Mac. If you're on Mac, give ⌥⇧F a try.

Answer from Zenexer on Stack Overflow
🌐
GitHub
github.com › vysker › vscode-php-formatter
GitHub - vysker/vscode-php-formatter: Visual Studio Code extension. A wrapper for the Sensiolabs PHP CS Fixer. Analyzes some PHP source code and tries to fix coding standards issues (PSR-1 and PSR-2 compatible).
After installing as an extension with Visual Studio Code, this extension automatically formats your PHP code, in accordance with PSR-0, PSR-1, PSR-2 or Symfony style conventions. Format current selection only, or the whole file.
Starred by 90 users
Forked by 12 users
Languages   TypeScript 89.5% | JavaScript 10.5% | TypeScript 89.5% | JavaScript 10.5%
🌐
GitHub
github.com › squizlabs › PHP_CodeSniffer › discussions › 3941
vs code formatting mixed php and html · squizlabs PHP_CodeSniffer · Discussion #3941
Adjust the settings for PHP and HTML formatting to your preference. For example: "php.format.enable": true, "php.suggest.basic": false, "editor.formatOnSave": true, // Enable format on save "editor.defaultFormatter": "esbenp.prettier-vscode", // Use Prettier as the default formatter ·
Author   squizlabs
Top answer
1 of 14
104

Update 2021-07-21

It's been more than half a decade since I first wrote this answer. The extensions to which I originally linked are abandoned, and Visual Studio Code's intrinsic PHP support hasn't improved, which is disappointing. The only decent extension still standing of which I'm aware is PHP Intelephense, which uses a freemium model: basic features are free, and a lifetime license is $12 USD as of writing.

The free version of Intelephense supports code formatting with the usual shortcuts (Alt + Shift + F on Windows and Linux, ⌥⇧F on macOS). Visual Studio Code continues to lack built-in support for PHP code formatting and will direct you to the extension marketplace if you attempt to format PHP without an appropriate extension installed.

Original answer

Visual Studio Code has pretty awesome PHP support. What it lacks is covered by extensions. A quick search reveals at least three (1, 2, and 3) that claim to support PHP formatting.

They mostly seem to use the standard shortcut of Alt + Shift + F on Windows/Linux, with varying shortcuts on Mac. If you're on Mac, give ⌥⇧F a try.

2 of 14
52

I installed

  • Prettier for HTML, CSS, and JavaScript files

  • PHP Intelephense for PHP files

I followed the instructions for each plugin but found I had to additionally edit settings.json manually in order to get them to work together.

"editor.defaultFormatter": "esbenp.prettier-vscode",
"[php]": {
  "editor.defaultFormatter": "bmewburn.vscode-intelephense-client"
}

The settings use Intelephense as the formatter for PHP files and use Prettier as the formatter for all other files.

Now I use Shift + Alt + F to format the files like everybody else.

🌐
GitHub
gist.github.com › superjojo140 › b498f9efdbce77dd3d3fbc7ece8f3536
PHP and HTML Formatting in VS Code · GitHub
php_formatting_vscode.md · Install ....com/items?itemName=rifi2k.format-html-in-php · Use the format-html-in-php to format the html snippets and phpfmt to format the php snippets ·...
🌐
GitHub
github.com › prettier › plugin-php
GitHub - prettier/plugin-php: Prettier PHP Plugin · GitHub
For example, if you are using PHP 7.4, but the plugin is set to PHP 8.3, it will produce code that uses features not available in PHP 7.4. A comment // prettier-ignore will exclude the next node in the abstract syntax tree from formatting. ... The official prettier plugin for vscode supports ...
Starred by 1.8K users
Forked by 139 users
Languages   PHP 67.1% | JavaScript 32.9%
🌐
GitHub
github.com › vysker › vscode-php-formatter › blob › master › README.md
vscode-php-formatter/README.md at master · vysker/vscode-php-formatter
After installing as an extension with Visual Studio Code, this extension automatically formats your PHP code, in accordance with PSR-0, PSR-1, PSR-2 or Symfony style conventions. Format current selection only, or the whole file.
Author   vysker
🌐
Reddit
reddit.com › r/phphelp › what is the most common php code formatter?
r/PHPhelp on Reddit: What is the most common PHP code formatter?
August 1, 2024 -

I found three code formatters for PHP.

  • Prettier (With PHP Plugin)

  • PrettyPHP (https://github.com/lkrms/vscode-pretty-php)

  • phpfmt (https://github.com/kokororin/vscode-phpfmt)

I was able to setup Prettier with the PHP plugin but the setup is not ideal since I have to install prettier and the prettier PHP plugin from NPM into the project every single time which is not the case when using Prettier VSCode extension with HTML, JS and CSS. I do like how Prettier has its own configuration .prettierrc files and it allows you to set a standard format for a project you are collaborating with others, however I believe formatting should be done in the IDE such as using a VSCode extension and this is the case with the Prettier extension for HTML, JS and CSS but not for PHP since it requires NPM packages.

The other two do not look popular. Am I missing something? I would like to have a standard format or be able to have an opinionated format setup like Prettier for JS but for PHP.

🌐
DEV Community
dev.to › ngobrolinit › prettier-for-php-file-formats-in-vscode-104o
Prettier for PHP File Formats in VSCode - DEV Community
March 18, 2024 - You can just read the Use section on the GitHub page to see how to use the plugin together with Prettier to format the PHP language. In my opinion, this method is quite complicated because it would be easier if the formatting was executed when the PHP file was saved (formatOnSave). To format the PHP language, I suggest you use the VSCode ...
Find elsewhere
🌐
GitHub
github.com › junstyle › vscode-php-cs-fixer
GitHub - junstyle/vscode-php-cs-fixer: PHP CS Fixer extension for VS Code
// Set PHP CS Fixer as the default formatter for PHP files // It will avoid conflict with other formatters like prettier etc.
Starred by 393 users
Forked by 51 users
Languages   JavaScript 95.8% | TypeScript 4.2% | JavaScript 95.8% | TypeScript 4.2%
🌐
GitHub
github.com › vysker › vscode-php-formatter › wiki
Home · vysker/vscode-php-formatter Wiki · GitHub
Visual Studio Code extension. A wrapper for the Sensiolabs PHP CS Fixer. Analyzes some PHP source code and tries to fix coding standards issues (PSR-1 and PSR-2 compatible). - Home · vysker/vscode-php-formatter Wiki
Author   vysker
🌐
GitHub
github.com › animus-coop › vscode-phpstorm-formatter
GitHub - animus-coop/vscode-phpstorm-formatter: PHPStorm Formatter integrated into Visual Studio Code.
Visual Studio Code Market Place: PHPStorm Formatter · Can also be installed in VS Code: Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter. ext install animus-coop.vscode-phpstorm-formatter · This extension ...
Author   animus-coop
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
PHP Formatter - Visual Studio Marketplace
Extension for Visual Studio Code - A wrapper for the Sensiolabs PHP CS Fixer. Analyzes some PHP source code and tries to fix coding standards issues (PSR-1 and PSR-2 compatible).
🌐
Jamesauble
jamesauble.com › blog › vs-code-php-formatter-that-actually-works-maclinux
VS Code PHP Formatter That Actually Works (Mac/Linux) - James Auble
February 24, 2022 - $ curl -L https://cs.symfony.com/download/php-cs-fixer-v2.phar -o php-cs-fixer · If for some reason CURL isn’t getting the file for you, go to the file’s Github Repo and download it however you want.
🌐
Aslam Doctor
aslamdoctor.com › home › articles › php code sniffing & formatting in vscode
PHP Code Sniffing & Formatting in VSCode - Aslam Doctor
June 16, 2025 - You can simply use it as a set of simple commands on the terminal. The linting tool provided is called phpcs and the formatting tool provided is called phpcbf. You will need composer to install PHP_CodeSniffer on your system.
🌐
GitHub
github.com › prettier › plugin-php › issues › 1721
How to install Prettier PHP Plugin in Visual Studio Code · Issue #1721 · prettier/plugin-php
May 6, 2021 - Hello, I am using Visual Studio Code for editing .php files. How can I set up Prettier plugin to format php in vs code. I tried to follow README.md but, I am not able to activate the code formattin...
Author   alexbalak21
🌐
Bordermedia
bordermedia.org › blog › using-prettier-format-php-in-vscode
Using Prettier to format PHP code in VS Code
Add Prettier as the default formatter for PHP in your VS Code settings.json: "[php]": { "editor.defaultFormatter": "esbenp.prettier-vscode", } Create a .prettierrc file in your project root directory: ... { "plugins": ["@prettier/plugin-php"], ...
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
PHP Formatter - pretty-php - Visual Studio Marketplace
Extension for Visual Studio Code - The opinionated PHP code formatter, now with full PHP 8.4 support
🌐
Devsense
docs.devsense.com › vscode › formatting
Code Formatting - PHP Tools for Visual Studio - Documentation
Formatter will help you keep your PHP code in VS Code clean and maintain the preferred code style like PSR-12, PSR-2, Allman and K&R.