Parse error: syntax error, unexpected (T_STRING), expecting variable (T_VARIABLE)

Parse error: syntax error, unexpected (T_STRING), expecting variable (T_VARIABLE)

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

Stefan Bogdanescu

Founder & Senior Architect

Parse error: syntax error, unexpected (T_STRING), expecting variable (T_VARIABLE)

When you encounter a Parse error in PHP, the language engine stops execution before the script even runs. This is distinct from a runtime error; the PHP parser literally cannot understand the code structure. In the specific case of unexpected (T_STRING), expecting variable (T_VARIABLE), the issue almost always points to a misunderstanding of how PHP handles class property declarations versus variable assignments.

The error log indicates that on the second line of your provided snippet, the parser encountered a string token (nomhotel) where it expected a variable assignment operator followed by a value. This happens because PHP syntax for declaring properties changed significantly between versions 5 and 7. Your code mixes old-style declaration syntax with new-style shorthand syntax, creating a conflict that triggers this specific error message.

class hotel extends WishDBxyz{ 
 public nomhotel;
 protected idhotel, ile_idile, pays_idpays, chainehotel_idchainehotel, actif ;
}

In PHP 8.0 and later, you can declare a property and initialize it in one step using the shorthand syntax: public $name = 'value';. If you write just public name; without the dollar sign, older versions of PHP might interpret name as a variable assignment expecting a $ prefix, or they might treat it as an incomplete statement. However, the specific error expecting variable (T_VARIABLE) usually implies the parser is looking for a $ character to start a property name but found a plain identifier instead, or vice versa depending on the exact version context.

The most likely culprit in your second line is the missing dollar sign if you are targeting PHP 5.x, or potentially a confusion regarding how multiple properties are declared. In modern PHP (8.0+), you can omit the $ when declaring properties, but you must still include the public, protected, or private keyword followed by the property name.

Consider this corrected version compatible with modern PHP standards:

class Hotel extends WishDBxyz {
    public string $nomHotel; // Explicitly typed and initialized later if needed
    protected int $idHotel;
    protected int $ileIdIle;
    protected string $paysIdPays;
    protected string $chaineHotelIdChaineHotel;
    protected bool $actif;
}

If you are working on a legacy codebase running PHP 7.4 or earlier, the syntax requires the dollar sign explicitly:

class Hotel extends WishDBxyz {
    public $nomHotel;
    protected $idHotel, $ileIdIle, $paysIdPays, $chaineHotelIdChaineHotel, $actif;
}

In your original snippet, you grouped multiple properties on a single line using commas. While this was valid in PHP 7 and earlier for declarations like public $foo, $bar;, mixing this with the shorthand syntax (omitting $) often confuses the parser if the version detection is ambiguous or if specific flags are set. The error message suggests the parser hit nomhotel without a preceding $ (in older contexts) or without a proper type hint structure in newer ones, leading it to expect a variable assignment context.

Another critical detail often overlooked by beginners is case sensitivity and naming conventions. While PHP properties are technically case-sensitive based on their definition, mixing PascalCase (nomHotel) with camelCase (idhotel) can lead to confusion when accessing them later or when generating database queries. Consistency helps prevent logic errors even if the syntax is correct.

If you are building a Laravel application, this specific error often appears when extending base classes or custom repository patterns. Laravel's Eloquent ORM relies heavily on strict property naming conventions to map to database columns. If your WishDBxyz parent class expects certain properties and your child class fails to declare them correctly due to syntax errors, you won't just get a parse error; you might also face hydration failures later at runtime.

To resolve this, ensure you are using the appropriate PHP version for your target environment. If you are unsure which version is running, add <?php echo phpversion(); ?> to a temporary file and run it via CLI. Upgrading to PHP 8.1 or higher allows for cleaner property declarations with explicit types, which acts as a safeguard against these kinds of syntax ambiguities.

For further reading on modern PHP features like union types and constructor property promotion, check out resources from https://laravelcompany.com where they often discuss best practices for modernizing legacy classes. However, the primary fix remains ensuring your property declarations match the version of PHP you are compiling against.

Our network