Getting Started with PHP_CodeSniffer
One of the first tools I recommend to developers of all experience levels when they're working with a team is PHP_CodeSniffer. After it's properly configured, it will catch coding style issues and minor bugs before your code even reaches a code review. Like most tools, when you first install it, the commands and configuration options can feel a little overwhelming. Let’s walk through what PHP_CodeSniffer is, what tools it provides, and how you can start using it effectively today.
What is PHP_CodeSniffer?
The PHP_CodeSniffer library provides us with the phpcs and phpcbf command-line tools. Both are command-line programs that check your PHP, JavaScript, and CSS files against defined coding standards:
phpcs - detects problems in your code by scanning your files and outputting violations
phpcbf - fixes problems where it can and will rewrite your files to fix formatting issues automatically
Think of phpcs as your code reviewer pointing out issues, and phpcbf as your virtual junior developer who fixes those issues.
Installing PHP_CodeSniffer
You can install PHP_CodeSniffer globally with Composer:
composer global require "squizlabs/php_codesniffer=*"
Alternatively, you can install it in your project. This is my preference as it can be used at the same version for everyone on the team and inside your CI/CD pipeline.
composer require --dev squizlabs/php_codesniffer
Now you can run them by typing vendor/bin/phpcs or vendor/bin/phpcbf. For the remainder of this article, we'll ignore the "vendor/bin" part, so you'll have to add that in (or alias it).
Using phpcs and phpcbf
To check a file with phpcs, run:
phpcs path/to/file.php
To fix a file with `phpcbf`, run:
phpcbf path/to/file.php
At the time of this writing, these commands default to the "PEAR" standard. However, your team might follow a different standard like "PSR12" or even a custom standard (see [Why Your Team Needs a Custom Coding Standard](https://scott.keck-warren.com/blog/2025/why-your-team-needs-a-custom-coding-standard) for why and check back later for how).
Specifying Your Standard at Runtime
To specify which standard to use at runtime, pass the `--standard` option:
phpcs --standard=PSR12 path/to/file.php
phpcbf --standard=PSR12 path/to/file.php
This tells PHP_CodeSniffer to check your file against PSR12 rules instead of the default. If you are using a custom standard, just use its name or path instead of "PSR12".
Setting Your Standard Globally
If you always want to use a specific standard, you can set it globally using the `--config-set` command:
phpcs --config-set default_standard PSR12
This sets your default standard to PSR12 for all future runs of phpcs and phpcbf without needing to pass the `--standard` flag each time.
You can check your current global config using the `--config-show` command line argument.
phpcs --config-show
Using config file: /Users/scottkeck-warren/Projects/Scott.KeckWarren.com/tmp/vendor/squizlabs/php_codesniffer/CodeSniffer.conf
default_standard: PSR12
Using phpcs in a Pre-commit Script
I'm a huge fan of using a [pre-commit script](https://scott.keck-warren.com/blog/2025/why-you-really-need-a-pre-commit-script-for-your-php-projects) for all of my projects, and phpcs is one of the tools I include early in that process so I can capture errors quickly.
Wrapping Up
PHP_CodeSniffer is a simple tool that will immediately improve your coding habits. Start by running phpcs on your projects to see where your code doesn’t align with your team’s standards. Then use `phpcbf` to fix what it can. Over time, you’ll find yourself writing cleaner code by default or at least code that is more consistent with your other code.
If you’re new to it, I recommend installing it in your current project today and running it on a file you recently worked on. It’s an easy win on your journey to writing better, cleaner, and more consistent PHP code.

