php 不能使用 [] 进行阅读

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3820258/
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 11:13:37  来源:igfitidea点击:

Cannot use [] for reading

phparraysfatal-error

提问by eriktm

In one of my scripts, I try to do the following

在我的一个脚本中,我尝试执行以下操作

$data[]?= self::get($row['sr_id']); // <-- line 55

However, PHP does not allow me to do this, giving me this error

但是,PHP 不允许我这样做,给我这个错误

Fatal error: Cannot use [] for reading in /file.php on line 55

致命错误:无法使用 [] 读取第 55 行的 /file.php

The self::getfunction return either a bool, or an object.

self::get函数返回一个布尔值或一个对象。

Edit:The get function creates a new object which again loads data from a mysql database.

编辑:get 函数创建一个新对象,该对象再次从 mysql 数据库加载数据。

采纳答案by mario

Old PHP versions accepted $var[]in expressions, allowed reading out the $varcontent regardless of syntax. PHP 5.1 made that illegal. But sometimes the error is triggered outside of the intented context.
So my guess (again: show more code)is that the preceeding line contains an unfinished expression, to which the $data[]joins.

$var[]表达式中接受旧的 PHP 版本,$var无论语法如何,都允许读出内容。PHP 5.1 使这成为非法。但有时错误是在预期上下文之外触发的。
所以我的猜测(再次:显示更多代码)是前一行包含一个未完成的表达式,$data[]连接到该表达式。

In case of object attribute you can wrap your $data var into { }, but that doesn't seem to be the problem in your case. (Else there is something in line 54, that you didn't show.) The right hand side can't reasonably trigger the error. Even array accessing []an integer or object wouldn't trigger that fatal error.

在 object 属性的情况下,您可以将 $data var 包装到 中{ },但这似乎不是您的问题。(否则,第 54 行中有一些您没有显示的内容。)右侧无法合理地触发错误。即使数组访问[]整数或对象也不会触发该致命错误。

So if nothing helps, just use array_push(). Work around PHP.

因此,如果没有任何帮助,请使用array_push(). 解决 PHP。

回答by SZL

The solution in my case was this:

在我的情况下的解决方案是这样的:

  • Bad line:
  • 坏线:

$this->$ExtraTag[] = $fullscript;

$this->$ExtraTag[] = $fullscript;

  • Good line:
  • 好线:

$this->{$ExtraTag}[] = $fullscript;

$this->{$ExtraTag}[] = $fullscript;

or

或者

$this->ExtraTag[] = $fullscript;

$this->ExtraTag[] = $fullscript;

回答by leooutlooks

The error I got was:

我得到的错误是:

Fatal error: Cannot use [] for reading in /pathtosite/drupal/sites/all/themes/zenui/templates/page.tpl.php on line 33

致命错误:无法使用 [] 读取第 33 行的 /pathtosite/drupal/sites/all/themes/zenui/templates/page.tpl.php

Sometime the problem is when you include a line like this:

有时问题是当您包含这样的行时:

$page['sidebar_first'][]

This might happen if you are copying a variable name and forgot to comment out the line.

如果您正在复制变量名称而忘记注释掉该行,则可能会发生这种情况。

There was two problems:

有两个问题:

1.Missing semicolon

1.缺少分号

2.$variable[] must set a variable

2.$variable[] 必须设置一个变量

After fixing these two problems my code read:

解决这两个问题后,我的代码如下:

$page['sidebar_first'][] = $value;

Don't forget to comment out line you are not using to help with the debugging process

不要忘记注释掉您不用于帮助调试过程的行

Hope this helps fellow programmers like me!

希望这能帮助像我这样的程序员!

回答by Nino Filiu

I had the same problem with my script, the following line threw the same error:

我的脚本遇到了同样的问题,以下行抛出了同样的错误:

$array[]=$value

I simply replaced it by

我只是将其替换为

$array[count($array)-1]=$value

and it worked perfectly.

它工作得很好。

回答by Byson

Another possible problem could be an accidental double ==. For example accidentally doing $myArray[] == $myNewValue;would cause this error (because you are trying to read a value with the ==instead of telling PHP to assign a new array index).

另一个可能的问题可能是意外的 double ==。例如,不小心这样做$myArray[] == $myNewValue;会导致此错误(因为您正在尝试使用 读取值==而不是告诉 PHP 分配新的数组索引)。

回答by Michael Eugene Yuen

I had same error with following:

我有以下相同的错误:

echo implode(',', $array[]);

which should have been

应该是

echo implode(',', $array);

Hope this can help someone

希望这可以帮助某人

回答by sathia

try:

尝试:

$data = Array();
$data[] = self::get($row['sr_id']); // <-- line 55