88

I get this PHP error:

Parse error: syntax error, unexpected T_VARIABLE

From this line:

$list[$i][$docinfo['attrs']['@groupby']] = $docinfo['attrs']['@count'];

Is there anything wrong with this line?

Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
omg
  • 123,990
  • 135
  • 275
  • 341
  • 1
    X-Ref: [PHP Parse/Syntax Errors; and How to solve them? - Unexpected T_VARIABLE](http://stackoverflow.com/a/18092267) for the other common causes and context-specific fixes. – mario Aug 18 '15 at 14:56

3 Answers3

198

There might be a semicolon or bracket missing a line before your pasted line.

It seems fine to me; every string is allowed as an array index.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
knittl
  • 197,664
  • 43
  • 269
  • 318
26

It could be some other line as well. PHP is not always that exact.

Probably you are just missing a semicolon on previous line.

How to reproduce this error, put this in a file called a.php:

<?php
  $a = 5
  $b = 7;        // Error happens here.
  print $b;
?>

Run it:

eric@dev ~ $ php a.php

PHP Parse error:  syntax error, unexpected T_VARIABLE in
/home/el/code/a.php on line 3

Explanation:

The PHP parser converts your program to a series of tokens. A T_VARIABLE is a Token of type VARIABLE. When the parser processes tokens, it tries to make sense of them, and throws errors if it receives a variable where none is allowed.

In the simple case above with variable $b, the parser tried to process this:

$a = 5 $b = 7;

The PHP parser looks at the $b after the 5 and says "that is unexpected".

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
dusoft
  • 10,672
  • 5
  • 35
  • 40
2

In my case it was an issue of the PHP version.

The .phar file I was using was not compatible with PHP 5.3.9. Switching interpreter to PHP 7 did fix it.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Zank
  • 41
  • 3