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...
Discussions

I'd like to run a syntax check on all PHP files changed before committing, or is this not best practice?
You can setup a pre-commit hook and run whatever checks you want. You should be able to get a list of the changes files by running git diff --name-only. More on reddit.com
๐ŸŒ r/git
19
8
December 11, 2019
Is it possible to hide the syntax-based errors from the browser?
The file's syntax is read first. If the syntax is incorrect, the file is not executed, so your ini_set calls are not made. These settings must be set at the server level (via php.ini) to hide these errors. More on reddit.com
๐ŸŒ r/PHPhelp
16
1
February 18, 2024
PHP Command Line Memory Limit - Help?
There is more than one version of php.ini, usually one for apache one for the command line. Are you sure you're looking at the right one? I don't know where it's located on a windows server, but on my linux system the command line file is /etc/php5/cli/php.ini and the one for apache is /etc/php5/apache2/php.ini More on reddit.com
๐ŸŒ r/PHP
16
1
February 3, 2010
๐ŸŒ
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
May 10, 2016 - 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
December 13, 2007 - 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.