phpparsingdebuggingsyntax-error

PHP parse/syntax errors; and how to solve them


Everyone runs into syntax errors. Even experienced programmers make typos. For newcomers, it's just part of the learning process. However, it's often easy to interpret error messages such as:

PHP Parse error: syntax error, unexpected '{' in index.php on line 20

The unexpected symbol isn't always the real culprit. But the line number gives a rough idea of where to start looking.

Always look at the code context. The syntax mistake often hides in the mentioned or in previous code lines. Compare your code against syntax examples from the manual.

While not every case matches the other. Yet there are some general steps to solve syntax mistakes. This references summarized the common pitfalls:

Closely related references:

And:

While Stack Overflow is also welcoming rookie coders, it's mostly targetted at professional programming questions.

If your browser displays error messages such as "SyntaxError: illegal character", then it's not actually -related, but a -syntax error.


Syntax errors raised on vendor code: Finally, consider that if the syntax error was not raised by editing your codebase, but after an external vendor package install or upgrade, it could be due to PHP version incompatibility, so check the vendor's requirements against your platform setup.


Solution

  • What are the syntax errors?

    PHP belongs to the C-style and imperative programming languages. It has rigid grammar rules, which it cannot recover from when encountering misplaced symbols or identifiers. It can't guess your coding intentions.

    Function definition syntax abstract

    Most important tips

    There are a few basic precautions you can always take:

    How to interpret parser errors

    A typical syntax error message reads:

    Parse error: syntax error, unexpected T_STRING, expecting ';' in file.php on line 217

    Which lists the possible location of a syntax mistake. See the mentioned file name and line number.

    A moniker such as T_STRING explains which symbol the parser/tokenizer couldn't process finally. This isn't necessarily the cause of the syntax mistake, however.

    It's important to look into previous code lines as well. Often syntax errors are just mishaps that happened earlier. The error line number is just where the parser conclusively gave up to process it all.

    Solving syntax errors

    There are many approaches to narrow down and fix syntax hiccups.

    If all else fails, you can always google your error message. Syntax symbols aren't as easy to search for (Stack Overflow itself is indexed by SymbolHound though). Therefore it may take looking through a few more pages before you find something relevant.

    Further guides:

    White screen of death

    If your website is just blank, then typically a syntax error is the cause. Enable their display with:

    In your php.ini generally, or via .htaccess for mod_php, or even .user.ini with FastCGI setups.

    Enabling it within the broken script is too late because PHP can't even interpret/run the first line. A quick workaround is crafting a wrapper script, say test.php:

    <?php
       error_reporting(E_ALL);
       ini_set("display_errors", 1);
       include("./broken-script.php");
    

    Then invoke the failing code by accessing this wrapper script.

    It also helps to enable PHP's error_log and look into your webserver's error.log when a script crashes with HTTP 500 responses.