PHP 中意外的 T_VARIABLE 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1464919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:42:59  来源:igfitidea点击:

What is unexpected T_VARIABLE in PHP?

phpsyntax-error

提问by omg

I get this PHP error:

我收到此 PHP 错误:

Parse error: syntax error, unexpected T_VARIABLE

解析错误:语法错误,意外的 T_VARIABLE

From this line:

从这一行:

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

Is there anything wrong with this line?

这条线有什么问题吗?

回答by knittl

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.

对我来说似乎很好;每个字符串都允许作为数组索引。

回答by dusoft

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

它也可能是其他一些线路。PHP 并不总是那么精确。

Probably you are just missing a semicolon on previous line.

可能您只是在前一行中缺少一个分号。

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

如何重现此错误,请将其放在名为的文件中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_VARIABLEis 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.

PHP 解析器将您的程序转换为一系列标记。AT_VARIABLE是 VARIABLE 类型的令牌。当解析器处理标记时,它会尝试理解它们,如果它收到一个不允许的变量,则会抛出错误。

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

在上面带有变量的简单情况下$b,解析器尝试处理:

$a = 5 $b = 7;

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

PHP 解析器查看 5 之后的 $b 并说“这是出乎意料的”。

回答by Zank

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

就我而言,这是 PHP 版本的问题。

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

我使用的 .phar 文件与 PHP 5.3.9 不兼容。将解释器切换到 PHP 7 确实修复了它。