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 OverflowCore 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?
Is there a way to prettify PHP code using VS Code?
How to install Prettier PHP Plugin in Visual Studio Code
How to make prettier (php) to work with VS Code - Stack Overflow
Formatting PHP code in vscode, using prettier, on save
No one should ever format on save, on projects where you work in a team, it will be an never ending whitespace war. And it will seriously pollute your VCS diff's with unrelated whitespace changes, and potentially create conflicts along the way. Please don't.
The only valid use case of reformatting on save is when it's a project requirement, everyone uses the same tooling, editor, and formatting rules. Under any other scenario, please don't.
More on reddit.comVideos
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
» npm install @prettier/plugin-php