Hướng dẫn format code php vscode

  • Trang chủ
  • Làm thế nào để format code trong Visual Studio Code (VSCode)

Làm thế nào để format code trong Visual Studio Code (VSCode)

Show

Để code của bạn trông đẹp và gọn gàng hơn. Bạn có thể dùng chức năng format code trong visual studio code (VSCode) với tổ hợp phím Shift + Alt +F  trên windows và Shift +Option +F trên Mac nhé.
Để dùng được tổ hợp phím này, trước tiên các bạn phải cài đặt plugin JS-CSS-HTML Formatter nhé.

Chúc các bạn thành công.

Tham khảo Live Server Visual Studio Code (VSCode)

Is it possible to get Visual Studio Code to format mixed HTML and PHP code on Windows Visual Studio Code version 0.7.1?

Hướng dẫn format code php vscode

asked Aug 26, 2015 at 20:22

steakoverflowsteakoverflow

1,1062 gold badges13 silver badges24 bronze badges

0

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.

answered Aug 27, 2015 at 1:33

16

For the best format setting for mixed PHP, HTML, and JavaScript code, just use 'PHP CS FIXER'.

And then use this simple configuration on your setting.json file:

"php-cs-fixer.executablePath": "${extensionPath}\\php-cs-fixer.phar",
"[php]": {
    "editor.defaultFormatter": "junstyle.php-cs-fixer",
    "editor.formatOnSave": true
},
"php-cs-fixer.rules": "@PSR2",
"php-cs-fixer.formatHtml": true,

Hướng dẫn format code php vscode

answered Apr 5, 2020 at 12:04

Hướng dẫn format code php vscode

BagaskaraBagaskara

7327 silver badges13 bronze badges

2

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.

answered Apr 27, 2021 at 0:01

bc1121bc1121

1511 silver badge5 bronze badges

4

The problem with most of the solutions is that they all are registered as formatting providers and within Visual Studio Code you can only run one formatter on a save for a specific file type.

So one may get you all the HTML, CSS, JavaScript code, but leave out the PHP code. Or if you use a regular PHP formatter there isn't one that exists that does the HTML correctly.

I went ahead and made an extension that runs before the save hook and isn't registered as a PHP formatter, so it will do all the HTML with js-beautify and then you can use something like PHPCS + PHPCBF to format the HTML. So it's basically, as far as I'm concerned, the best solution currently available.

Format HTML in PHP on the Visual Studio Code marketplace.

Hướng dẫn format code php vscode

answered Jul 25, 2018 at 0:25

Hướng dẫn format code php vscode

rifi2krifi2k

1891 silver badge6 bronze badges

0

I use bmewburn.vscode-intelephense-client and it works perfectly (other than if there's an syntax error).

There is a premium version; I don't know how well that works.

On GitHub

On VSCode Marketplace

Official Site


The Format HTML in PHP extension that many other people mention does work, except there are some alignment issues, and you have to format the HTML and the PHP separately.


phpcbf is also a good choice, except it requires more customization, and it's a bit more complicated to figure out. That could be a good thing or a bad thing, depending on what you want.

answered Dec 8, 2020 at 14:40

RedGuy11RedGuy11

3255 silver badges14 bronze badges

1

I've tried just about every formatting extension for Visual Studio Code, and I don't think there is a single one that can handle correctly/consistently formatting of mixed PHP/HTML pages.

Hướng dẫn format code php vscode

answered Apr 15, 2020 at 17:04

0

I'm coding in Laravel and I am using Format HTML In PHP for formatting the HTML code among the PHP code. It's working perfectly.

RedGuy11

3255 silver badges14 bronze badges

answered Mar 11, 2019 at 14:58

Hướng dẫn format code php vscode

1

Go to extension in vs code:

Hướng dẫn format code php vscode

then write: category:formatters php

press enter. you will see all available formatter. at the moment install the one you like.

Hướng dẫn format code php vscode

then right click inside any opened php file chose format document from the popup menu.

answered Jun 19, 2021 at 15:04

Hướng dẫn format code php vscode

2

Add the extension Format HTML in PHP to Visual Studio Code.

Or search in extensions with "format HTML in PHP".

After reload, use Shift + Alt + F.

Hướng dẫn format code php vscode

answered Nov 13, 2019 at 7:35

After installed Extensions. Settings > Search >formatting and enable 'Format On Save' then edit setting.json file as below


    "editor.defaultFormatter": "esbenp.prettier-vscode",
      "[php]": {
        "editor.defaultFormatter": "bmewburn.vscode-intelephense-client",
        "editor.formatOnSave": true
      },
      "editor.formatOnSave": true

Save >Restart IDE this worked for me.. :)

answered Sep 25 at 9:37

Hướng dẫn format code php vscode