php-cs-fixer https://github.com/PHP-CS-Fixer/PHP-CS-Fixer I do disagree on code standards being enforced in the IDE. My view is they absolutely need to be enforced at the project level to ensure consistency of code standard throughout that project. The IDE should be capable of configuring itself based on project owned configurations (this is the case when using php-cs-fixer). Edit: To your point of having an opinionated standard set out of the box. PSR12 https://www.php-fig.org/psr/psr-12/ . A rulset for this standard already exists in php-cs-fixer. Edit 2: For absolute transparency, there is also another popular capable of doing everything I mentioned above - https://github.com/squizlabs/PHP_CodeSniffer Answer from JParkinson1991 on reddit.com
๐ŸŒ
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.

๐ŸŒ
Reddit
reddit.com โ€บ r/vscode โ€บ can't get php formatter to work
r/vscode on Reddit: Can't get PHP formatter to work
December 1, 2020 -

Hi.

I've installed Junstyle' "php-cs-fixer" to be able to format my .php files, but can't get it to work. When i right click the .php file and press "Format Document", none of the indentation error are corrected.

I'm on linux, and these are pretty much the steps I've taken:

  1. composer global require friendsofphp/php-cs-fixer

  2. Updated settings.json, setting entries such as this: "php-cs-fixer.executablePath": "/home/me/.config/composer/vendor/friendsofphp/php-cs-fixer/php-cs-fixer",

  3. Installed the VS Code extension "php cs fixer" from Junstyle

Clearly I'm missing something, but can't figure out why, so any help debugging this will be appreciated.

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.

๐ŸŒ
Reddit
reddit.com โ€บ r/vscode โ€บ vs code extension for formatting a php file with html?
r/vscode on Reddit: VS Code extension for formatting a PHP file with HTML?
September 6, 2025 -

I usually use PHP Intelephense to format code. but when I try to format code with html, the php code lines up with the beginning of the code block like the first image. this can result in a huge amount of indentation. It's hard to read and manage.

I tried another extension, Format HTML in PHP, it simply skips tabbing in PHP. see image 2.

I would like the code to look similar to image 3, adding a single indentation when going into or out of php code.

I realize that I can put the php start and end on it's on line, like image 4, but this NOT what I want to do. it affects the HTML and can affect the display of page by adding spacing.

are there any extensions that can accomplish this?

๐ŸŒ
Reddit
reddit.com โ€บ r/wordpress โ€บ format php in vs code on windows?
r/Wordpress on Reddit: Format PHP in VS Code on Windows?
June 15, 2018 -

Hey all,

I am a junior Front End dev who somehow got himself roped into making Wordpress sites. Yikes. Jokes aside, it's been a wild ride and I am looking forward to being a part of the global WP community.

I wanted to ask for advice with something that has been driving me up the wall. I gave into pressure from my boss (read: did the right thing) and install the Wordpress coding standard. From what I've seen of it, I don't really agree with it, but it's what the team uses so, let's take one for the team.

However! To this day, I have been unable to find a PHP Formatter for VS Code on Windows. Now I have this linter screaming at me all the time and I am losing my mind. Please help! How do you format your PHP code in VS Code?

๐ŸŒ
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).
Find elsewhere
๐ŸŒ
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
๐ŸŒ
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 - 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 extension called PHP Intelephense which ...
๐ŸŒ
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
๐ŸŒ
Reddit
reddit.com โ€บ r/phphelp โ€บ best php extension for vs code?
r/PHPhelp on Reddit: Best php extension for vs code?
August 7, 2022 -

I switched from vscode + intelephense to phpstorm and i find it reeeeaaaally hard to go back because of all of the functionality that storm offers

However, i love vscodes snappiness and ease of use that phpstorm will never match

Is there any way out? How do I combine both? Im willing to pay for an extension just to get best of both worlds

๐ŸŒ
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
๐ŸŒ
Reddit
reddit.com โ€บ r/vscode โ€บ what php formatter to psr-12 php do you recommend ?
r/vscode on Reddit: What PHP formatter to PSR-12 PHP do you recommend ?
January 13, 2022 - Hey, As a student, I need to be sure that my code respects the PSR-12 standards. So I'm looking for a good PHP formatter to help me โ€œfixโ€ my code orโ€ฆ
๐ŸŒ
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/vscode โ€บ best auto formatter extension?
r/vscode on Reddit: Best Auto Formatter Extension?
October 17, 2021 -

So I keep seeing Prettier come up when I look for an auto-formatter but I'm a little worried as it says that it's opinionated.

My workplace has very specific coding standards and I'd prefer one that is configurable as possible.

Also preferably with the ability to share those settings with other members of my team.