<!--
  canonical-url: https://www.avonture.be/blog/online-php-linter
  published:     2024-12-01
  This is a static plain-Markdown mirror generated at build time.
  Visit the canonical URL above for the fully rendered page, with images and interactive components.
-->

# 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!

---
![Format poorly formatted PHP code](/img/v2/clean_code.webp)

**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?

**my\_collection.php**

```php
<?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;
}}
}
```

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](https://laravel.com/docs/13.x/pint) tool (but not limited to Laravel code for sure).

So, here's how the code looked before:

![Correctly formatted PHP code](./images/before.webp)

Jump to [Hexmos Pint formatter](https://hexmos.com/freedevtools/tldr/common/pint), copy the code into the **Code** textarea of the script and let the reformatting happen:

![Correctly formatted PHP code](./images/after.webp)

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](/blog/php-jakzal-phpqa) gives you `php-cs-fixer` and `phpcbf` without installing anything, and [Git - pre-commit-hooks](/blog/git-precommit) runs them before each commit.

## If you're a PHP developer

... then make sure to use tools like [PHP-CS-Fixer](https://github.com/PHP-CS-Fixer/PHP-CS-Fixer) or [PHP\_CodeSniffer](https://github.com/PHPCSStandards/PHP_CodeSniffer) or many others. See also my [Docker image that provides static analysis tools for PHP](/blog/php-jakzal-phpqa).

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.
