PHP's CLI includes a linting feature that checks a specified file for syntax errors, allowing for quick verification of PHP files or snippets before execution.
Example:
php -l file.php
The above command will check for syntax errors and return the same.
No syntax errors detected in file.php
Before 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. However, with PHP 8.3, it is possible to pass multiple PHP files, and PHP CLI will lint all of them in the same invocation.
php -l Creator.php Filesystem.php NamespaceResolver.php Str.php
No syntax errors detected in Creator.php
No syntax errors detected in Filesystem.php
No syntax errors detected in NamespaceResolver.php
No syntax errors detected in Str.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 fail the lint, it exits with the following error.
- If there is any file that is not accessible, the exit code will be 1.
- If any of the files fail to lint, the exit code will be 255.
- When both error conditions are present, the exit code will be 1.
Also, it supports global patterns to lint multiple files
php -l src/*.php src/**/*.php