45

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

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

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

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

Nikhil Vaghela
  • 2,088
  • 2
  • 15
  • 30
eriktm
  • 752
  • 1
  • 6
  • 17

7 Answers7

34

The solution in my case was this:

  • Bad line:

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

  • Good line:

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

or

$this->ExtraTag[] = $fullscript;

Community
  • 1
  • 1
SZL
  • 341
  • 1
  • 3
  • 2
  • Sweet. $this->{$ExtraTag}[] = $fullscript worked for me! – FateNuller Jun 22 '15 at 16:01
  • 1
    I'm not surprised the first "bad" line doesn't work and the last line does: the "bad" line is actually invalid syntax, and the last line is the intended syntax. The middle example is an ugly workaround to turn the first example into the third. I would recommend against that: it is less clear, less concise, and less performant (because PHP doesn't have a precompiler to optimise it away to the third example). – Byson Jul 21 '17 at 13:06
  • Great, but this doesn't solve the OP question. – Studocwho Jul 14 '23 at 03:00
24

Old PHP versions accepted $var[] in expressions, allowed reading out the $var content 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.

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.

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

aksu
  • 5,221
  • 5
  • 24
  • 39
mario
  • 144,265
  • 20
  • 237
  • 291
2

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

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

2. $variable[] must set a 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!

Nikhil Vaghela
  • 2,088
  • 2
  • 15
  • 30
1

I had same error with following:

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

which should have been

echo implode(',', $array);

Hope this can help someone

Michael Eugene Yuen
  • 2,470
  • 2
  • 17
  • 19
0

try:

$data = Array();
$data[] = self::get($row['sr_id']); // <-- line 55
Rostyslav Dzinko
  • 39,424
  • 5
  • 49
  • 62
sathia
  • 2,192
  • 2
  • 24
  • 42
0

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.

Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
0

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).

Byson
  • 540
  • 4
  • 19