Skip to main content

Format poorly formatted PHP code

Need to clean up messy PHP code fast? Discover the best online PHP linter, Pint-express (based on Laravel Pint), to instantly format and beautify your code. No installation needed!

· 3 min read
View raw
Christophe
Markdown, WSL and Docker lover ~ Insatiable curious.
Cet article existe aussi en français — lire la version française.

Changelog

Latest
Replaced dead pint-express link (benjamincrozat.com/pint-express, 404) with hexmos.com/freedevtools/tldr/common/pint; updated squizlabs→PHPCSStandards; Laravel docs 11.x→13.x.

Format poorly formatted PHP code

⚡
TL;DR

This article points to Pint-express (benjamincrozat.com/pint-express), an online PHP formatter based on Laravel Pint, for instantly cleaning up poorly formatted PHP code by pasting it into a textarea — no install needed. For ongoing projects, it recommends wiring up PHP-CS-Fixer or PHP_CodeSniffer in the editor, build process, or CI pipeline instead.

You're recovering some old PHP code; you want to answer a question asked on a forum and the person who posted the PHP question didn't take care to ensure that it was properly formatted; ... there are too many occasions when the syntactic quality of the code can be rotten.

You'd like to have an online tool that you can use to quickly retrieve code with a much more polished layout, just by copying and pasting. Without having to install anything and without any headaches.

For example, how do you make the code below look cleaner in five seconds?

<?php
abstract class mycollection implements Iterator
{
protected function populate($array, $dataobject){
foreach($array as $item){
$object = new $dataobject();
foreach($item as $key => $val){$object->$key = $val;}
$this->storage[] = $object;
}}
}

This abstract class provides a helper method that converts an array of data into a collection of objects. The populate method loops through an array, instantiates a new object of a specified class for each item, copies the array data into that object's properties, and stores it in an internal collection.

There are a huge number of linters on the internet, but this is perhaps one of the best: https://hexmos.com/freedevtools/tldr/common/pint. It's based on the Laravel Pint tool (but not limited to Laravel code for sure).

So, here's how the code looked before:

Correctly formatted PHP code

Jump to Hexmos Pint formatter, copy the code into the Code textarea of the script and let the reformatting happen:

Correctly formatted PHP code

Much better.

Note: there are other tools like for instance https://codebeautify.org/php-beautifier.

An online tool is perfect for a one-off paste. For your own projects, wire the formatter into your workflow instead: jakzal/phpqa gives you php-cs-fixer and phpcbf without installing anything, and Git - pre-commit-hooks runs them before each commit.

If you're a PHP developer​

... then make sure to use tools like PHP-CS-Fixer or PHP_CodeSniffer or many others. See also my Docker image that provides static analysis tools for PHP.

Remember to add these tools to your editor (there are many VSCode extensions), to your workflow (f.i. by using local make actions), or add these steps to a remote pipeline.


Was this article helpful?
Did you try the steps in this article?