Guide Errors & Troubleshooting

How to fix a PHP parse or syntax error

PHP could not even read the file, let alone run it — here is how to use the exact line it names to find the real mistake.

Updated 6 min read Beginner

A parse error looks like this: "Parse error: syntax error, unexpected token '...', expecting ',' or ';' in /home/site/public_html/wp-content/themes/example/functions.php on line 42". It means PHP tried to read the file and found something that breaks the language's grammar — a missing punctuation mark, a bracket that never closed, a stray character — before it ever got to running a single line of it.

This is, in one specific way, the easiest PHP error to fix: it names the exact file and line, and the fault is almost always a small, mechanical mistake rather than a logic problem. It is also one of the more alarming, because it typically takes the whole site down at once rather than affecting one feature.

Nothing is corrupted

A parse error means one file has a typo in it, not that anything has been damaged. The moment that one file is corrected, the site returns exactly as it was.

Go straight to the file and line named in the error

Open the file manager in your control panel, navigate to the exact path given, and open that file at the line number shown — most file managers and code editors display line numbers directly, which makes finding the right spot immediate.

The mistake is often above the reported line, not on it

PHP reports where it first realised the file no longer made grammatical sense, which is frequently the line after the actual problem. A function missing its closing brace will parse correctly right up until PHP hits the next piece of code and has nowhere valid to put it — so if the named line looks perfectly correct, check the handful of lines immediately above it before looking anywhere else.

What to look for at that line

1. A missing semicolon on the previous line

The single most common cause. PHP requires a semicolon at the end of most statements, and a missing one is invisible to the eye but breaks parsing at the very next line, which is usually the one actually reported in the error.

2. An unmatched bracket, brace, or parenthesis

Count opening and closing { }, ( ), and [ ] characters around the reported area. An editor with bracket matching — most code editors and even some file manager text editors have this — will jump straight to the partner of any bracket you click, which makes a mismatch obvious immediately rather than requiring you to count by eye.

3. Smart quotes copied from a word processor or web page

Code copied from a tutorial, a PDF, or a document editor frequently carries curly quotation marks (' ' " ") instead of the straight ones PHP requires (' and "). These look nearly identical at normal reading size and are one of the most common causes of a parse error that seems to come from nowhere. Retype the quote marks directly in a plain text editor rather than relying on find-and-replace, which can sometimes reintroduce the same substitution.

4. An unclosed string

A quotation mark opened but never closed — often because the string itself contains an apostrophe that wasn't escaped — causes PHP to keep reading everything that follows as part of the string, until it runs out of file and reports an unexpected end. An apostrophe inside a single-quoted string needs a backslash before it: 'it\'s broken' rather than 'it's broken'.

5. A stray closing PHP tag or extra character

An accidental ?> partway through a block of code, or a character pasted in from outside the editor, can break parsing in a way that looks like nothing is wrong at a glance. If the file was edited outside a proper code editor — in an email client, a chat window, or a word processor — retype the change directly rather than pasting it.

The fastest fix while you find the real one

If the site needs to be back up immediately and you don't yet have the fix, restore the previous working version of the file from a backup, or via version control if the site uses it. That returns the site to working order while you fix the error at leisure, rather than under pressure with the site down.

If you can't find the actual mistake

Copy the exact error message, including the full file path and line number, along with the contents of that file. That's normally all it takes for support to spot the specific character causing it, particularly for the smart-quote and stray-character cases that are easy to miss by eye.

Frequently asked questions

Why did this happen with no warning?

Because a parse error only appears the moment PHP actually tries to load the file containing the mistake. If a file was edited, uploaded, or restored with an error already in it, everything looks fine until the next time that specific file is loaded — which on a live site can be the very next visitor.

The line PHP names looks completely correct. Why?

Because PHP reports where it noticed the file no longer made sense, which is often a line or several lines after the actual mistake — a missing closing brace, for instance, is only detected once PHP reaches something that cannot follow without it. Read upward from the reported line as well as at it.

Related reading