Visual Studio Code (VSCode) is a popular code editor renowned for its versatility and numerous extensions that enhance the coding experience. Among these, the
Prettier extension stands out for its ability to format code consistently across various languages, making it a favourite among developers. However, when it comes to formatting PHP files, many users encounter challenges, as Prettier does not natively support PHP formatting. This limitation can be frustrating, especially for those looking to maintain a clean and organised codebase in PHP.
In this tutorial, I will guide you through the steps to effectively set up your VS Code environment to automatically format PHP files using Prettier in conjunction with the
PHP Intelephense extension. By following this guide, you'll be able to streamline your PHP development process and ensure your code looks sharp and professional. Let’s dive in and unlock the full potential of code formatting in VSCode!
2. Set as default formatter in VS Code settings:
a) File - Preferences - Settings / Shortcut: CTRL + ,
b) Search: Formatter
c) Editor: Default Formatter select: Prettier - Code Formatter
4. CTRL + Shift + P then search for "open user settings (JSON)" and insert the following parameter:
{
... //other config
"[php]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "bmewburn.vscode-intelephense-client"
},
"editor.linkedEditing": true
...
}
All files ending with the .php extension will be formatted using the PHP Intelephense extension. Make sure to securely save the settings.json file to preserve these configurations for future use.