php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #17290 Cannot use [] for reading
Submitted: 2002-05-17 11:02 UTC Modified: 2002-08-11 01:00 UTC
Votes:33
Avg. Score:3.9 ± 1.1
Reproduced:31 of 31 (100.0%)
Same Version:7 (22.6%)
Same OS:15 (48.4%)
From: mellow at mellow dot dk Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 4.1.2 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mellow at mellow dot dk
New email:
PHP Version: OS:

 

 [2002-05-17 11:02 UTC] mellow at mellow dot dk
I've build an array using the following in a function :

-------------
$this->get_data(array("","","and D.XiParentId= 'gwak_root_element' ORDER BY D.vcOrder"));
        $arrCategory = $this->return_array();
        for($i = 0 ; $i < sizeof($arrCategory) ; $i++) {
            $this->get_data(array("","","and D.XiParentId = '" . $arrCategory[$i]["XiDataId"] . "' ORDER BY D.vcOrder"));
            $arrItem[$arrCategory[$i]["vcItemId"]] = $this->return_array();
        }
return $arrItem;
-------------

This constructs an array with values like :
$arrItem["project_priority"][0] = array(...)
$arrItem["project_priority"][1] = array(...)
etc...

Later I tried to do the following to the returned $arrItem :
-------------
$arrItem["project_severity"][] = array("XiDataId" => "gwak_get_all", "vcText" => "All");
-------------

This gives the error :
-------------
Fatal error: Cannot use [] for reading in /home/plb/public_html/gwak/module/project/include/Task.class.php on line 160
-------------

Funny because doing the following :
-------------
        $arrThis["here"][] = array("XiDataId" => "asdasdas", "vcText" => "324234234");
        $arrThis["here"][] = array("XiDataId" => "asdasdas", "vcText" => "324234234");
        $arrThis["here"][] = array("tr" => "ert");
        $arrThis["where"][] = array("er" => "ert");
        $arrThis["where"][] = array("we" => "ert");
        $arrThis["where"][] = array("tr" => "ert");

-------------
gives no error at all, eventhough $arrThis and $arrItem have exactly the same format. Do you guys have an idea what is going on here?

The only thing i can see could cause the problem is that $arrItem is built using "arrays within arrays":
$arrItem[$arrCategory[$i]["vcItemId"]] = array(...);
Can this be the cause or...

In advance thx...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-05-24 06:08 UTC] witterstein at web dot de
I occured a similar error when using (also on lx-machine)

while ($db->next_record())
{
/* Fix for php 4.2.1 */
	$foo=$db->f('RIS_VSR_FK');
	$curstate[$foo] = $db->f('RIS_ALLOWED');
/* End Fix
	$curmain[$db->f('RIS_VSR_FK')] = $db->f('RIS_ISMAIN'); <-!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
	$defmainvs = $db->f('RIS_ISMAIN') == 'YES' ? $db->f('RIS_VSR_FK') : $defmainvs;
}

Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ']' in xxx on line 166 <-!!!!!!!!!!!! this line

That means that accessing an object in a class instance and using this as the arrays index also does not work, using a simple variable does work.

Probably there is a major bug in the parser (priority?), because the workaround with $array[{$array_b[1][2]}], which I would expect that first {$array_b[1][2]} is evaluated and then $array[$evaluation_result] gets the error that no {} are allowed. I am not sure this is a new bug, but I remember in 4.0.x it always worked fine.
 [2002-06-26 14:09 UTC] robjohnwilson at hotmail dot com
I seem to be getting this error also in 4.2.1 running on SunOS - is this possible?
 [2002-07-10 23:25 UTC] sniper@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.


We need a self-contained and SHORT example script which
shows the problem..

 [2002-08-11 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-03-18 21:17 UTC] 7l9it46r1adm1l1 at jetable dot com
I got the same strange Fatal error (using Windows XP, PHP PHP 5.0.4):
Fatal error: Cannot use [] for reading in ... on line 82

[php]
if ($insert->check($_POST['value']) === FALSE)
{
   $error[] == 'Error'; // line 82
}
[/php]

Zend Developer didn't give any warnings so there shouldn't be any syntax mistakse in that PHP file.
 [2006-04-18 19:03 UTC] oliver at samera dot com dot py
Seems to be a class related bug, here is a simple test-case:

<?php
// php 4.3.9 - CentOS 4.3 - oliver@samera.com.py - 18-apr-2006

// -----------------------------------------------------------------------------
class cache_db {
	var $resultado_array;

	function cache_db() {
		$this->$resultado_array = array();
	}

	// abre un archivo y lo carga en memoria
	function cache_open() {
		$archivo_array = array(array('1'), array('2'), array('3'));
		$resultado_array = array();

		foreach($archivo_array as $linea) {
			$resultado_array[] = $linea;
			$this->$resultado_array[] = $linea;
		}
	}
}

// without a class
$archivo_array = array(array('1'), array('2'), array('3'));
$resultado_array = array();

foreach($archivo_array as $linea) {
	$resultado_array[] = $linea;
}

?>
 [2006-06-12 13:01 UTC] Matt_B at TinyOnline dot co dot uk
I've just had the same problem, from changing a variable from global, to class specific. After some confusion I realised I forgot to remove the $, just like in your example:

$this->$resultado_array[] should be
$this->resultado_array[]

and then it all works fine.
 [2007-07-20 21:56 UTC] asohn at aircanopy dot net
PHP 4.3.4
I got this error when mistakenly used "return $variable[];" instead of "return $variable" in a user defined function.
 [2008-09-16 17:22 UTC] naugtur at gmail dot com
I've had the same problem. It reported the error in line 26, but the line was ok. I often try entering empty lines - it reveals if the error is somewhere else. The problem with this error is just trying to read something like this:
$somename[]

if you try:
$this=$thatarray[]; 
without putting something between [] you get this error. 

Everything would be ok if PHP reported it in correct line. Please fix that! It would save people ages of looking the code through!
 [2008-11-27 12:29 UTC] tomis at centrum dot cz
If the problem is not the obvious one
i.e. $x = $my_array[]; which is wrong because empty [] are only for setting, there can be this case(as posted by [18 Apr 2006 7:03pm UTC] oliver at samera dot com dot py)
it is not a bug, but a way how PHP treats calling of variables
simple test case 
class A {
public $field = array();
public $field2 = array();
public function AddToField($fieldname, $item){
$this->$fieldname[] = $item;
}
}
this throws an error because PHP is trying to read fieldname WITH the parenthsis 
so the workaround is to put 
$this->{$fieldname}[]=$item and it is working - it just helps compiler what did you ment with $fieldname.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 19:01:29 2024 UTC