Parse error: syntax error, unexpected '$result' (T_VARIABLE)

Parse error: syntax error, unexpected '$result' (T_VARIABLE)

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

Stefan Bogdanescu

Founder & Senior Architect

Parse error: syntax error, unexpected '$result' (T_VARIABLE)

The immediate culprit behind this parse error is a fundamental misunderstanding of how scope works in PHP functions. The error message specifically points to line 76 (in the context of the original file), which corresponds to the line where $result is assigned. However, looking at the code provided, the actual syntax failure happens earlier, inside the function definition itself.

The issue lies in the very first line of the newUser function:

$email = $this->verify('Email',$email,10,40);

Inside a standard PHP function, you do not have access to $this. The $this keyword is reserved for classes and objects. When the PHP parser encounters this line inside a function defined outside of a class context or without an object instance passed to it, it throws a fatal error. While your immediate error message mentions unexpected '$result', this often happens because the parser fails to properly parse the preceding lines due to earlier syntax errors or scope issues, causing the error report to jump ahead in the stack trace depending on the PHP version and specific runtime state.

To fix this, you must ensure that $this is only used within a class method. If newUser is intended to be a standalone function, remove all references to $this. Alternatively, if this logic belongs inside a class, move the entire function definition into the class body as a method.

Refactoring for Scope and Security

Beyond the immediate syntax error, the code snippet contains several critical security vulnerabilities that must be addressed before attempting to fix the parsing issue. The current implementation uses mysql_* functions, which are deprecated and removed in PHP 7.0. More importantly, constructing SQL queries by concatenating variables directly into the query string is a recipe for SQL injection attacks.

Here is a corrected version of the logic that addresses the scope error and introduces basic safety:

function newUser($email, $pwd, $pwd2, $firstname, $surname, $isAdmin = 0) {
    // Validate email length manually or via regex since we can't use $this here
    if (strlen($email) < 10 || strlen($email) > 40) return -1;
    
    if (strlen($pwd) < 6 || strlen($pwd) > 20) return -1;
    if ($pwd !== $pwd2) return -1;

    // Capitalize names manually without relying on undefined class methods
    $firstname = ucfirst(strtolower($firstname));
    $surname = ucfirst(strtolower($surname));

    // Generate a secure key, not an MD5 hash of a constant string
    $key = bin2hex(random_bytes(16));

    // Use Prepared Statements to prevent SQL Injection
    $stmt = $db->prepare("INSERT INTO user (email, pw, firstname, surname, isAdmin) VALUES (?, ?, ?, ?, ?)");
    
    // Note: AES_ENCRYPT is a database function. Depending on the driver, 
    // you might need to handle encryption differently or execute it within the DB layer.
    $stmt->bind_param('sssss', $email, $pwd, $firstname, $surname, $isAdmin);
    
    if ($stmt->execute()) {
        return $db->insert_id;
    }

    return 0;
}

The Transition to Modern PHP Frameworks

If you are maintaining legacy code that relies on procedural MySQL functions and custom class structures like $this->verify, it is highly recommended to refactor toward a modern framework. Custom validation logic scattered across methods often leads to the exact scope issues seen here.

Frameworks like Laravel provide robust, built-in mechanisms for exactly these problems. They handle database connections safely using Eloquent ORM or Query Builders, which automatically manage prepared statements and parameter binding. Validation rules are centralized, removing the need for repetitive length checks inside every function.

For developers looking to modernize their stack, LaravelCompany offers resources on building secure applications that avoid these legacy pitfalls entirely. Moving away from procedural code not only fixes parse errors related to $this but also significantly hardens your application against injection attacks and data integrity issues.

Summary of Fixes

  1. Scope Correction: Ensure $this is used only within class methods.
  2. Deprecation Removal: Replace mysql_* functions with PDO or MySQLi.
  3. SQL Injection Prevention: Use prepared statements (? placeholders) instead of string concatenation.
  4. Security Hardening: Stop using md5("secure"); use cryptographically secure random bytes for salts or keys.

Our network