You could simply do shell_exec() like this:

$output = shell_exec('php -l /path/to/filename.php');

This gives you the output of the command line operation in the string $output.

Answer from Mike Brant on Stack Overflow
🌐
PHP
php.net › manual › en › features.commandline.php
PHP: Command line usage - Manual
Put that line in its own file and ... prepended to any PHP file run from the command line. ... use " instead of ' on windows when using the cli version with -r php -r "echo 1" -- correct php -r 'echo 1' PHP Parse error: syntax error, unexpected ''echo' (T_ENCAPSED_AND_WH...
🌐
Software Testing Lead
softwaretestinglead.com › home › how to use a php syntax checker for bug-free code
How to Use a PHP Syntax Checker for Bug-Free Code – Software Testing Lead
February 28, 2025 - A PHP Syntax Checker is a tool that analyzes PHP code for syntax errors. It highlights mistakes and suggests fixes. Developers use it to prevent runtime errors.
🌐
Lindevs
lindevs.com › check-syntax-errors-in-multiple-php-files-at-once-in-php-8-3
Check Syntax Errors in Multiple PHP Files at Once in PHP 8.3 | Lindevs
December 2, 2023 - The CLI (Command Line Interface) of PHP offers a static code analysis feature (also known as linter) with the -l option, allowing the user to check a specified PHP file for syntax errors.
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-do-syntax-checking-using-php
How to do syntax checking using PHP ? - GeeksforGeeks
September 21, 2021 - We can use --syntax-check in place of -ln, it is more readable. ... It returns "No syntax errors detected in <filename>" if the provided PHP file has no syntax errors and passed the syntax check.
🌐
BairesDev
bairesdev.com › tools › phpcodechecker
PHP Code Checker - Syntax Check for Common PHP Mistakes
An advanced, custom PHP code checker that searches your code for common, hard to find typos and mistakes; includes a syntax check.
🌐
PHPHub
phphub.net › home › php code checker
PHP validator
April 11, 2024 - POST https://api.phphub.net/php-checker/{PHP version} Return a list of syntax errors. PHP versions: 8.5.0, 8.3.6, 8.0.0, 7.4.8 or 5.6.40 ... { "code": 255, "stdout": "\nParse error: syntax error, unexpected identifier \"cho\" in file on line 3\nErrors parsing file\n" }
🌐
Lobaugh
ben.lobaugh.net › blog › 202647 › quick-recursive-php-file-syntax-check
Quick recursive PHP file syntax check
I find it handy to run a PHP syntax (lint) check on files after resolving a merge conflict in git. Here is a handy snippet that will run find every file in the current and sub-directories and run them through the PHP command line syntax checker.
Find elsewhere
🌐
Uoa
cgi.di.uoa.gr › ~rouvas › php-manual › function.php-check-syntax.html
php_check_syntax
The php_check_syntax() function performs a syntax (lint) check on the specified filename testing for scripting errors. This is similar to using php -l from the commandline except php_check_syntax() will execute (but not output) the checked file_name.
🌐
W3Schools
w3schools.com › php › php_syntax.asp
PHP Syntax
boolval() debug_zval_dump() doubleval() is_countable() empty() floatval() get_defined_vars() get_resource_type() gettype() intval() is_array() is_bool() is_callable() is_double() is_float() is_int() is_integer() is_iterable() is_long() is_null() is_numeric() is_object() is_real() is_resource() is_scalar() is_string() isset() print_r() serialize() settype() strval() unserialize() unset() var_dump() var_export() PHP XML Parser · utf8_decode() utf8_encode() xml_error_string() xml_get_current_byte_index() xml_get_current_column_number() xml_get_current_line_number() xml_get_error_code() xml_parse
🌐
Daring Fireball
daringfireball.net › 2003 › 12 › php_syntax_checking_in_bbedit
Daring Fireball: PHP Syntax Checking in BBEdit
December 6, 2003 - Here’s how it works. If you pass the php command-line tool the -l option (that’s a lowercase L), it will perform a rudimentary syntax check instead of executing the source code.
🌐
Fandom
vim.fandom.com › wiki › Runtime_syntax_check_for_php
Runtime syntax check for php | Vim Tips Wiki | Fandom
"wincmd w" endfunction :setl makeprg=php :set errorformat=%m\ in\ %f\ on\ line\ %l " Map <CTRL>-P to check the file for syntax :noremap <C-P> :call PHPsynCHK()<CR>
🌐
Server Fault
serverfault.com › questions › 392875 › how-can-i-check-the-syntax-of-another-php-script-in-php-before-it-is-included
How can I check the syntax of another PHP script in PHP before it is include()'d - Server Fault
May 26, 2012 - Instead, use php -l somefile.php from the commandline. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Community Asks Sprint Announcement – January 2026: Custom site-specific badges!
🌐
DaniWeb
daniweb.com › programming › web-development › threads › 100875 › php-syntax-checking
PHP Syntax checking | DaniWeb
Since I can't run it, I just took a look at the web site. I see that it does syntax checking, but it looks like it's just the basic syntax checking you can do from the command line with "php -l".
🌐
PHPStan
phpstan.org › user-guide › command-line-usage
Command Line Usage | PHPStan
Increases the verbosity and makes PHPStan show various debugging information like consumed memory or why result cache is not used. Combining -vvv with --debug is great for identifying slow files. Running with -vvv will also print same information as the diagnose command.
🌐
LinkedIn
linkedin.com › all › application architecture
How can you debug a PHP application with syntax errors?
September 25, 2023 - For example, if you have a file called test.php, you can run the following command to check it for syntax errors: php -l test.php The PHP CLI will output either "No syntax errors detected in test.php" or "Parse error: syntax error, unexpected ...
🌐
ArticleCube
articlecube.com › improve-your-php-code-quality-syntax-checkers
Improve Your PHP Code Quality with Syntax Checkers | ArticleCube
April 11, 2025 - By narrowing down errors to specific lines and types, syntax checkers make debugging faster and less stressful. Advanced syntax checkers can warn you about deprecated features or syntax that may not be compatible with newer PHP versions. When working in a team, using a standardized syntax checker ensures everyone follows the same coding conventions, reducing merge conflicts and misunderstandings. This built-in command-line tool lets you check PHP files for syntax errors.
🌐
Alan Dix
alandix.com › blog › 2010 › 06 › 09 › php-syntax-checker-updated
PHP syntax checker updated | Alan Dix
June 9, 2010 - The syntax checker is a pretty simple layer over the PHP command line option “php -l” and also uses the PHP highlight_file function.