Parse Error: syntax error: unexpected '{'

Parse Error: syntax error: unexpected '{'

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Parse Error: syntax error: unexpected '{'

When the PHP interpreter throws a "Parse error: syntax error, unexpected '{'", it is screaming that your code structure violates the language's strict rules before it even attempts to execute logic. In the snippet you provided, the error points directly to line 11, which corresponds to the header call inside the nested if block. However, looking closer at the syntax reveals a deeper issue: the entire conditional logic surrounding that line is malformed.

The specific line causing the crash is header ('Location: ./userhome.php'). Notice what follows it? Nothing. There is no semicolon. In PHP, a statement must terminate with a semicolon unless it is part of a block defined by curly braces {} or a closing tag. Because the line is missing its terminator, the parser assumes the next token, else, belongs to the header statement. It then attempts to parse the else block as if it were arguments for the header function, leading to the "unexpected '{'" error when it finally encounters the opening brace of the subsequent block.

However, fixing just that one semicolon is only a partial solution because the logic inside the condition on line 12 is fundamentally broken. The code reads:

if (md5($password) == !strpos($file, (md5($password))) {

This statement attempts to compare a hash string against the boolean result of strpos. In PHP, strpos returns an integer index or false if not found. The expression !strpos(...) converts that return value into a boolean (true if the user wasn't found, false otherwise). You are then comparing a string (the MD5 hash) to a boolean using the loose equality operator (==). While PHP will perform type juggling here, this logic is nonsensical. You likely intended to check if the calculated password hash matches the raw username or some stored value, not the result of an existence check on the file.

Furthermore, mixing echo with header() calls in the same script is a security and usability antipattern. As soon as PHP outputs any data—whether via echo, print, or even HTML—it sends a response header to the browser. Once headers are sent, you can no longer call header('Location: ...'). If the user's username isn't found in the database check on line 5, the script echoes an error message. When the code reaches line 12, it is too late; the headers have already been flushed to the client. The redirect will fail silently, and the browser will display the "Redirecting..." text along with any subsequent output.

To resolve this, you must separate your logic flow and fix the syntax errors. First, ensure every statement ends with a semicolon. Second, validate that no output has occurred before attempting a redirect. Third, refactor the condition to make logical sense. Here is a corrected version of the snippet:

<?php
    $username = $_POST['username'] ?? '';
    $password = $_POST['pwd'] ?? '';

    // Validate inputs exist before processing
    if (empty($username) || empty($password)) {
        echo "Please provide both username and password.";
        exit;
    }

    $file = file_get_contents("userdb.html");
    
    // Check if username exists first
    if (!$file || !strpos($file, $username)) {
        echo "Your username was not found in our database. Please go back and try again.";
        exit; // Stop script execution here to prevent further logic errors
    }

    // Calculate hash and verify against stored data
    $calculatedHash = md5($password);
    
    // Assuming userdb.html contains the expected hash string
    if (strpos($file, $calculatedHash) !== false) {
        // Ensure headers are not sent before this point
        header('Location: ./userhome.php', true); 
        exit;
    } else {
        echo "Whoops! Your password seems to be incorrect. Go back and try again.";
    }
?>

In production environments, relying on a plain text file like userdb.html for authentication is a critical security flaw. Storing passwords in readable HTML files exposes them to any user who can read the source code or access the raw file. You should migrate this logic to a dedicated database and implement prepared statements to prevent SQL injection.

If you are building a larger application, consider using a framework like Laravel. The LaravelCompany ecosystem provides robust authentication scaffolding that handles session management, password hashing with bcrypt, and CSRF protection out of the box. Migrating from this procedural PHP pattern to an MVC structure will not only eliminate syntax errors caused by misplaced braces but also significantly harden your application against common web vulnerabilities.

Our network