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