How to Check PHP Syntax Errors (php -l, VS Code, Online)

How to Check PHP Syntax Errors (php -l, VS Code, Online)

A php syntax checker answers one question: will the engine parse this file? The authoritative command is php -l. FlashKit’s PHP code syntax checker is a quicker, weaker first pass: JavaScript heuristics in this tab, no PHP binary, no upload. Treat it like a spell-check, then lint with the runtime you actually ship. Markup and SQL are different jobs — HTML5 validation and the SQL formatter do not parse PHP.

php -l and what it does not catch

Ran php -v on 10 Nov 2026:

PHP 8.3.6 (cli) (built: Oct 19 2026 15:16:58) (NTS)
Zend Engine v4.3.6

php -l file.php tokenizes and parses. On success it prints No syntax errors detected in file.php. On failure it prints a PHP Parse error: and Errors parsing file.php, exit status non-zero. It does not execute the file. It does not load autoloaders. It will not tell you that foo() is undefined, that a type hint was violated, or that a query string is wrong. For those, use PHPStan or Psalm in CI, on the same major version as production.

Quick in-tab pass (not php -l)

Line-based JS: missing semicolons on echo/return/assignments, brace/paren counts, missing <?php. Local, no PHP engine.

Open the PHP syntax checker

Decode 8 common parse errors with snippets

Each snippet was saved and run through php -l on PHP 8.3.6. The quoted messages are the CLI’s words, not a paraphrase.

1. Missing semicolon before the next statement

<?php
$name = "World"
echo "Hello $name";

php -l: PHP Parse error: syntax error, unexpected token "echo" in /tmp/php-lint-samples/01-missing-semicolon.php on line 3. PHP notices the next token, not the missing ; on line 2. Add the semicolon on the assignment. FlashKit’s heuristic will flag line 2 as “Possible missing semicolon (; )” because it sees = and no terminator — that is the one case where the JS pass is more specific than php -l.

2. Unclosed brace

<?php
function greet($name) {
    return "Hello $name";

PHP Parse error: Unclosed '{' on line 2 in /tmp/php-lint-samples/02-unclosed-brace.php on line 4. Close the function. The checker counts { vs } across the paste and reports Unclosed brace "}". Missing 1. on the last line.

3. Unclosed string

<?php
$msg = "Hello World;
echo $msg;

PHP Parse error: syntax error, unexpected end of file in /tmp/php-lint-samples/03-unclosed-string.php on line 4. The quote started on line 2 never ended, so the rest of the file is still a string. FlashKit does not parse quotes; php -l is the check that catches this.

4. Extra closing brace

<?php
function greet($name) {
    return "Hello $name";
}
}

PHP Parse error: Unmatched '}' in /tmp/php-lint-samples/04-extra-brace.php on line 5. Delete the stray }. The JS pass reports Extra closing brace "}" detected.

5. Unclosed parenthesis in a call

<?php
echo strtoupper("hello";

PHP Parse error: syntax error, unexpected token ";", expecting ")" in /tmp/php-lint-samples/05-unclosed-paren.php on line 2. Close the strtoupper( call. FlashKit’s paren counter reports Unclosed parenthesis ")". Missing 1.

6. if / else without braces, missing semicolon

<?php
if (true) echo "ok" else echo "no";

PHP Parse error: syntax error, unexpected token "else", expecting "," or ";" in /tmp/php-lint-samples/06-unexpected-echo.php on line 2. The alternative syntax needs a semicolon after echo "ok", or use braces. FlashKit skips lines that start with if, so this file can look “clean” in the online box and still fail php -l. That is the honesty line: the online php syntax checker is not the parser.

7. Missing comma in an array

<?php
$a = [
    "one"
    "two",
];

PHP Parse error: syntax error, unexpected double-quoted string "two", expecting "]" in /tmp/php-lint-samples/07-array-missing-comma.php on line 4. Put a comma after "one". The JS heuristic looks for = / echo / return / print and will not necessarily flag a missing array comma.

8. Two statements, first missing semicolon

<?php
$a = 1
$b = 2;

PHP Parse error: syntax error, unexpected variable "$b" in /tmp/php-lint-samples/08-unexpected-variable.php on line 3. Same class as snippet 1: the error is reported on the following token. Add ; after 1.

VS Code / PhpStorm linting

VS Code with a PHP file: the built-in language features plus an extension such as Intelephense underline parse errors while you type. Point php.validate.executablePath (or Intelephense’s environment) at the same binary you run in CI — 8.3 here. PhpStorm: Settings → PHP language level matching production, then the Inspections for syntax. Neither replaces php -l on the CI image; they shrink the time to the first red squiggle. Format-on-save is a style tool, not a parser.

Git pre-commit hook

Scratch repo at /tmp/php-hook-demo on 10 Nov 2026, hook .git/hooks/pre-commit:

#!/bin/sh
files=$(git diff --cached --name-only --diff-filter=ACM | grep '\.php

 || true)
[ -z "$files" ] && exit 0
ok=0
for f in $files; do
  php -l "$f" || ok=1
done
exit $ok

good.php was <?php echo "ok"; — commit succeeded. bad.php was <?php echo "bad" (no semicolon) — php -l printed a parse error and the hook blocked the commit. Install the hook as executable. CI should still run php -l (or PHPStan) because hooks are local and skippable with --no-verify.

Online checker walkthrough and privacy note

The FlashKit page is two columns: a textarea labelled PHP Script, and Linting Results. The default sample already has a missing semicolon after echo "Hello $name" so you can see an issue on load. There is no Run on a server. useEffect re-runs the JS pass on every keystroke. It flags:

  • Lines containing =, or starting with echo / return / print, that do not end in ; { } : , . and are not if/while/for/foreach/class/function openers — “Possible missing semicolon (;).”
  • A missing <?php anywhere in the paste.
  • Brace and parenthesis counts (unclosed or extra).

It skips lines that look like comments (//, #, *). It does not execute PHP. There is no network call in the component — the paste stays in the tab. Still: do not paste production secrets. After the JS pass is green, run php -l. For queries, the MySQL syntax checker is the sibling first pass, same caveat versus the real engine.

What the JS pass will miss, besides snippets 3, 6 and 7: typed properties, union types, named arguments, first-class callables, a missing use import, an undefined function, a wrong argument count, and anything inside a string that looks like code. It also false-positives: a return on one line and the expression continued with . concatenation on the next is fine in PHP and may still warn if the first line has return without a terminator the heuristic likes. Treat green in the online box as “no obvious missing semicolon or brace,” not “this file parses on 8.3.6.”

PHPStan and Psalm sit after php -l. They need Composer, a level, and the same platform PHP as production. They will catch strlen(null) and missing return types that php -l never sees. CI should run php -l on every changed .php file on the deploy image, then PHPStan. A pre-commit hook is a courtesy to the author. Editor squiggles are a courtesy to the author. The merge gate is the CI image.

Then validate the HTML around it

PHP lint does not check markup. Use the HTML5 validator on the template output.

Open the HTML5 validator

Related Tools