- Home
- PHP Syntax Checker
PHP Syntax Checker
Paste PHP code to find syntax errors, with the exact message and line from PHP's own parser. Check one version or all of them, from 7.4 to 8.5.
<?php
// Paste PHP code and press "Check syntax". Nothing is executed.
function greet(string $name): string
{
$parts = ['Hello', $name]
return implode(', ', $parts) . '!';
}
echo greet('World');
Press Check syntax or ⌘/Ctrl + Enter. Pick All to check every version from 7.4 to 8.5.The code is parsed in your browser and never executed or uploaded.
How this PHP syntax checker works
The checker loads the real PHP interpreter, compiled to WebAssembly, and hands your code to PHP's parser with token_get_all($code, TOKEN_PARSE). That is the same parser that runs when PHP loads a file on your server. If the parser stops, you get its exact error message and line number, and the line is highlighted in the editor. If it succeeds, your code is syntactically valid for that version.
Because the parser differs from version to version, the same file can be valid on PHP 8.4 and broken on PHP 7.4. Choose All to parse your code with all seven versions and see at a glance which ones accept it. A syntax check does not prove that code works: calling a function that does not exist, passing the wrong type or using a removed function are runtime errors. The PHP Version Checker runs your code on every version to catch those.
Common PHP syntax errors and how to fix them
PHP 8 changed the wording of parse errors from token names like T_STRING to readable phrases like unexpected identifier. Both versions are listed where they differ.
syntax error, unexpected end of fileA block was opened and never closed. Count your { and }, and check for an unclosed string, heredoc or comment above the reported line. The line number points at the end of the file, not at the real mistake.
syntax error, unexpected token "return"Almost always a missing semicolon on the line before. PHP only notices the problem when it reaches the next statement, so look one line up. The sample code in the checker above has exactly this error.
syntax error, unexpected identifier "…"A word appears where PHP expects an operator or a semicolon. Common causes are a missing $ on a variable, a missing concatenation dot between a string and a variable, or a missing comma in an array or argument list.
syntax error, unexpected variable "$…"Two expressions sit next to each other without an operator, for example echo $a $b. Add the missing ., comma or semicolon.
syntax error, unexpected token "}"An extra closing brace, or a statement before it that is not finished. Check the line above for a missing semicolon or closing parenthesis.
syntax error, unexpected '=>' (T_DOUBLE_ARROW)On PHP 7.4 this usually means the code uses match or another PHP 8 feature. The older parser does not know the syntax. Run the check with All selected to see which versions accept it.
syntax error, unexpected token "enum"Enums need PHP 8.1. On PHP 8.0 and older, enum is not a keyword, so the declaration cannot be parsed.
Unterminated comment starting line …A /* comment has no closing */. Everything after it was treated as a comment.
Syntax that needs a newer PHP version
If code parses on your computer but fails on the server, the server is probably older. These are the syntax changes most likely to cause a parse error on an older version:
| Syntax | Needs PHP |
|---|---|
| Arrow functions fn() =>, typed properties, ??= | 7.4+ |
| match, nullsafe ?->, named arguments, attributes #[…], constructor promotion | 8.0+ |
| enum, readonly properties, never, first-class callables strlen(...) | 8.1+ |
| readonly class, DNF types (A&B)|null, true/false/null types | 8.2+ |
| Typed class constants const string X | 8.3+ |
| Property hooks, asymmetric visibility public private(set) | 8.4+ |
| Pipe operator |>, closures in constant expressions | 8.5+ |
Checking syntax on your own machine
PHP has a built-in lint mode. In a terminal, run php -l file.php for one file. It prints No syntax errors detected or the parse error with its line. To lint a whole project except the vendor folder:
find . -name "*.php" -not -path "./vendor/*" -print0 | xargs -0 -n1 php -l
Most editors do this automatically. VS Code with the PHP Intelephense extension and PhpStorm underline syntax errors as you type. In continuous integration, the php-parallel-lint package lints thousands of files in seconds, and static analysers such as PHPStan and Psalm go further by checking types, undefined methods and dead code.
Frequently asked questions
Does the syntax checker run my code?
No. It only parses the code, the same first step php -l performs. Nothing is executed, so it is safe to check code that deletes files, sends email or loops forever. To run code, use the PHP Sandbox.
Is this the same as php -l?
Very close. Both use PHP's own parser, so every parse error is caught with the same message and line number. php -l also compiles the code, which catches a few extra compile-time errors such as declaring the same function twice or using break outside a loop. Running the code in the sandbox catches those as well.
Why is the error reported on the wrong line?
PHP reports the line where it noticed the problem, which is often after the real mistake. A missing semicolon is reported on the next statement; an unclosed brace is reported at the end of the file. Look at the reported line and the few lines above it.
Can I check code for an older PHP version?
Yes. Choose a version from 7.4 to 8.5 in the switcher, or choose All to check every version in one go. This tells you whether your syntax is compatible with an older server before you deploy.
How do I check the syntax of a whole project?
Run php -l on every file from a terminal: find . -name "*.php" -not -path "./vendor/*" -exec php -l {} \;. For deeper checks than syntax, use a static analyser such as PHPStan or Psalm, which find type errors and calls to functions that do not exist.