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.
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:
-
composer global require friendsofphp/php-cs-fixer -
Updated settings.json, setting entries such as this:
"php-cs-fixer.executablePath": "/home/me/.config/composer/vendor/friendsofphp/php-cs-fixer/php-cs-fixer", -
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.
Videos
Hi, I'm new to VS Code.
Is there a way to prettify my PHP code? Any extensions or a hidden function to do the job?
Thank you very much! :)
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.
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.
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?
I find some extensions format it but it still looks weird. What's the best and preferably one that formats on save?
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?
Hi All,
I am trying to format my code with shift+option+f but I get the response: there is no document formatter for 'php'-files installed
Anyone know of a reliable code formatter add-on or how to make the built in code formatter work?
Thanks!
You can define that key sequence to execute the Format document command, and then try this extension: https://marketplace.visualstudio.com/items?itemName=Sophisticode.php-formatter
Disclaimer: I found that extension with a simple search, and donโt have any experience with it.
https://marketplace.visualstudio.com/items?itemName=jerrygoyal.shortcut-menu-bar
vscode isn't formatting my php code when I hit ALT+SHIFT+F. Is anyone else having this issue?
Thanks!
Phidler
I typed some code similar to yours, and found there was no such option, but changing it to HTML, you can [wrongly] format it. No idea if that is by design, bug, or what, but I'd try https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-intellisense and see if that helps
Here is an example : http://imgur.com/NcEMIuW
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
I've been mainly using VSCode for React and NodeJs, but since i now started writing PHP couple of months ago, i noticed that the prettify support for VSCode and PHP is really bad. There are some beta versions but need special setups.
How do you work with PHP and VSCode?
(Dont recommend Code editors as PHPStorm etc. - thanks)
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.