Core prettier does not support PHP1, and thus neither does the plugin for VSCode/VSCodium. Luckily the designers of prettier seems to have thought of this and implemented a plugin system2.
According to the prettier-vscode repo, all you need to do to use a plugin is to add it and prettier to your package.json3
So for php support your package.json would need to contain:
{
"devDependencies": {
"@prettier/plugin-php": "0.14.3",
"prettier": "2.0.5"
}
}
Answer from fredrik on Stack Overflowhtml - VSCode Prettier not formatting PHP - Stack Overflow
Is there a way to prettify PHP code using VS Code?
How to make prettier (php) to work with VS Code - Stack Overflow
What is the most common PHP code formatter?
Core prettier does not support PHP1, and thus neither does the plugin for VSCode/VSCodium. Luckily the designers of prettier seems to have thought of this and implemented a plugin system2.
According to the prettier-vscode repo, all you need to do to use a plugin is to add it and prettier to your package.json3
So for php support your package.json would need to contain:
{
"devDependencies": {
"@prettier/plugin-php": "0.14.3",
"prettier": "2.0.5"
}
}
Prettier for HTML, CSS, and JavaScript files
change your settings.json
"editor.defaultFormatter": "esbenp.prettier-vscode",
// add this code
"[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.
if you dont know ho to open setting.json lets see : I usually just press ctrl + , to get to settings and then click the "Open Settings (JSON)" icon that appears in the top right hand corner under the title bar Here's another option... VS Code: How to open settings.json file? How can I open Visual Studio Code's 'settings.json' file?
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! :)
From the official doc here: https://github.com/prettier/plugin-php for Visual Studio Code: add php to document selectors in vscode settings (settings.json)
"prettier.documentSelectors": [
"**/*.{js,jsx,ts,tsx,vue,html,css,scss,less,json,md,mdx,graphql,yaml,yml,php}"
]
and declare the php parser in prettier config file (.prettierrc) https://prettier.io/docs/en/configuration
{
"parser": "php",
"plugins": ["@prettier/plugin-php"]
}
Try installing the PHP formatter locally, like this:
npm install --save-dev prettier @prettier/plugin-php
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.
» npm install @prettier/plugin-php