PHP error guide

PHP Parse Error: syntax error, unexpected token

Error summary

PHP could not parse the file—usually a missing `;`, mismatched brace/parenthesis, or syntax from a newer PHP version running on an older interpreter.

What it means

PHP could not parse the file—usually a missing ;, mismatched brace/parenthesis, or syntax from a newer PHP version running on an older interpreter.

What the error means

Compilation failed before the script ran. The reported line is where the parser got confused; the real mistake may be earlier.

Why PHP produces it

PHP parses whole files (and some templates) before execution. Invalid tokens abort the request with a parse error.

PHP version notes

Older PHP may say unexpected T_STRING / unexpected T_VARIABLE instead of unexpected token.

Most common causes

  • Missing semicolon or comma
  • Unclosed (, [, {, or string
  • Using PHP 8+ syntax on PHP 7
  • Merge conflict markers left in a file

Minimal examples

BAD — reproduces the problem

php
$x = 1\n$y = 2; // missing semicolon on previous line confuses parser

FIXED — safer pattern

php
$x = 1;\n$y = 2;

Step-by-step diagnosis

  1. Open the reported file/line and scan upward for unclosed structures.
  2. Run php -l path/to/file.php for a quick lint.
  3. Compare the server PHP version with the code’s required version.

Fixes

Fix #1: Lint the file

Use the CLI linter matching the deployment PHP version.

bash
php -l app/Router.php

Fix #2: Align syntax with runtime

Either upgrade PHP or remove newer syntax (named args, union types, etc.).

php
// Ensure runtime matches language features used in the file

Common mistakes when fixing it

  • Editing the wrong file because an opcode cache serves an old path

How to prevent it

  • CI php -l over the tree
  • Pin PHP version in platform config

Web server / environment notes

Deployments where build PHP ≠ runtime PHP are common triggers

Tags: cli,fpm,shared-hosting

Categories

Relevant ZendStudio.net tools