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.
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...
Top answer 1 of 8
6
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.
2 of 8
5
I use token_get_all for this. I have some PHP code in the db. Before saving, I do
function is_valid_php_code_or_throw( $code ) {
$old = ini_set('display_errors', 1);
try {
token_get_all("<?php\n$code", TOKEN_PARSE);
}
catch ( Throwable $ex ) {
$error = $ex->getMessage();
$line = $ex->getLine() - 1;
throw new InvalidInputException("PARSE ERROR on line $line:\n\n$error");
}
finally {
ini_set('display_errors', $old);
}
}
Works like a charm. Syntax only. No missing variables, type incompayibility etc.
InvalidInputException is my own. You can make it anything, or return a bool, or handle the exception yourself.
I'm not sure if display_errors is necessary. It was at some point.
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.
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!
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.
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.