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
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.

๐ŸŒ
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
๐ŸŒ
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).
๐ŸŒ
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.
๐ŸŒ
GitHub
github.com โ€บ squizlabs โ€บ PHP_CodeSniffer โ€บ discussions โ€บ 3941
vs code formatting mixed php and html ยท squizlabs PHP_CodeSniffer ยท Discussion #3941
Open your VS Code settings (press Ctrl + , or go to File > Preferences > Settings). Search for "format" in the search bar to filter formatting-related settings. Adjust the settings for PHP and HTML formatting to your preference.
Author ย  squizlabs
๐ŸŒ
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 - To format the PHP language, I suggest you use the VSCode extension called PHP Intelephense which was created by Ben Mewburn. Install the extension first. After the extension is installed, you can go to File -> Preferences -> Settings, then look ...
Find elsewhere
๐ŸŒ
Bordermedia
bordermedia.org โ€บ blog โ€บ using-prettier-format-php-in-vscode
Using Prettier to format PHP code in VS Code
Using Prettier to format PHP code in VS Code provides several benefits: Consistency across multiple languages in your project ... The @prettier/plugin-php plugin makes it easy to extend your existing Prettier setup to include PHP files, maintaining ...
๐ŸŒ
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 - Open any .php file in VS Code right click in the editor and click on Format Document. If that formatted your code then great!
๐ŸŒ
Codethepixel
codethepixel.com โ€บ articles โ€บ Formatting-PHP-in-VSCode-Using-Prettier-and-PHP-Intelephense
CTP: Formatting PHP in VSCode Using Prettier and PHP Intelephense
Learn how to set up Visual Studio Code (VSCode) for automatic PHP file formatting using the Prettier and PHP Intelephense extensions. This step-by-step tutorial will help you maintain a clean and organised codebase, enhancing your PHP development experience.
๐ŸŒ
Visual Studio Marketplace
marketplace.visualstudio.com โ€บ items
Format HTML in PHP - Visual Studio Marketplace
Extension for Visual Studio Code - Provides formatting for the HTML code in PHP files using JSbeautify - Works well paired with a PHP formatting extension
๐ŸŒ
W3Docs
w3docs.com โ€บ php
Format code command for PHP/HTML in Visual Studio Code
Click the "Format" button to apply the formatting to the selected text. Alternatively, you can also use the keyboard shortcut Ctrl+Shift+I to automatically format the code in the current document.
๐ŸŒ
Aslam Doctor
aslamdoctor.com โ€บ home โ€บ articles โ€บ php code sniffing & formatting in vscode
PHP Code Sniffing & Formatting in VSCode - Aslam Doctor
June 16, 2025 - 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 โ€บ 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).
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). - vysker/vscode-php-formatter
Starred by 90 users
Forked by 12 users
Languages ย  TypeScript 89.5% | JavaScript 10.5% | TypeScript 89.5% | JavaScript 10.5%
๐ŸŒ
Visual Studio Marketplace
marketplace.visualstudio.com โ€บ items
PHP Sniffer & Beautifier for VS Code
Extension for Visual Studio Code - PHP Sniffer & Beautifier for Visual Studio Code
๐ŸŒ
Nasiothemes
nasiothemes.com โ€บ format-php-code-in-visual-studio-code-wordpress-coding-standards
Format PHP Code in Visual Studio Code (WordPress Coding Standards) โ€“ Nasio Themes
I tried all VSC extensions, however, I could not find a single one that did what it needed to โ€“ properly format php code as per WordPress coding standards. For example, the popular PHP formatter VSC extension formats the opening bracket of a function at a new line: