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"]
}
Answer from Didier Corbière on Stack OverflowFrom 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
Videos
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! :)
» npm install @prettier/plugin-php