PHP 8.3: PHP CLI Lint (php -l
) supports linting multiple files at once
PHP's CLI provides a Linting feature that checks a passed file name for syntax errors. This is helpful to quickly check a PHP file or a snippet before executing.
php -l my-file.php
No syntax errors detected in my-file.php
Prior to PHP 8.3, it was not possible to lint multiple PHP files in the same invocation; regardless of the number of files provided, PHP CLI only linted the first file:
php -l file1.php file2.php
No syntax errors detected in file1.php
Since PHP 8.3, it is possible to pass multiple PHP files and PHP CLI lints all of them in the same invocation.
php -l file1.php file2.php
No syntax errors detected in file1.php
No syntax errors detected in file2.php
If the linter finds any errors in any of the provided files, it shows the error, and continues to the rest of the list. If any of the files failed the lint, it exists with an error
Glob Patterns Are Supported
In addition to passing multiple file names individually, it is also possible to use a glob pattern to lint multiple files:
php -l src/*.php src/**/*.php
Exit Codes
- If there is any file that was not accessible, the exit code will be
1
. - If any of the files failed to lint, the exit code will be
255
. - When both errors conditions are present, the exit code will be
1
.
Backward Compatibility Impact
Prior to PHP 8.3, PHP CLI silently ignored additional files passed to lint. For Glob patterns, it used only the first file.
For PHP Applications that only run on PHP 8.3+, it is now possible to quickly lint multiple PHP files or an entire code base easily without having to resolve to calling the Linter multiple times (with find
command for example) or third-party tools.
Third-party tools such as php-parallel-lint/PHP-Parallel-Lint
can provide parallel linting that works across multiple PHP versions.