Format Multiple PHP Files with Preview

Author by Jakub Misek
4 minutes

The PHP extension for Visual Studio Code comes with an exhaustive code formatting built-in (see docs). There is a vast array of parameters to configure your custom code style, predefined code styles for common standards, and all of this with ability to only format selection, format-on-type, format-on-save, and newly - format multiple PHP files at once, with preview.


How To Format Multiple PHP Files

This feature is available through Command Palette (F1) in Visual Studio Code. Pick the command "PHP > Format Multiple Files":

format multiple php files command

The command asks for a glob pattern. Specify what .php files you want to format and confirm.

glob of files to format in batch

Confirm and watch the progress in Visual Studio Code's notification area. The formatting runs on background, it does not open the documents in your document area, and does not modify any files yet.

Once the formatting is done, a preview window with all the suggested changes is opened.

Preview Changes

The list of suggested changes is listed in a preview window at the bottom of Visual Studio Code:

list formatting changes

Click on an item in the list to see the diff window of checked changes:

formatting changes diff window

Click Apply to confirm and do the changes to selected files.

Configuration

The formatting respects the settings for code formatting rules.

php.format.codeStyle: Choose from one of many standards such as psr-12, laravel, K&R, wordpress, drupal etc. (free)

php.format.rules. ...: Many custom formatting rules to adjust. See customize formatting for the details. This feature comes as a premium for our customers.

php.format.exclude: Glob patterns of files excluded from formatting.

See Also