Fatal Error - Cannot use string offset as an array...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Fatal Error - Cannot use string offset as an array...

Post by Benjamin »

I'm dealing with someone else's code here, and for some reason in one of the files, PHP will randomly throw errors on multidimensional arrays. Even if it's not set I should only get a notice. Anyone ever have this happen before?

Code: Select all

echo 'blah' . $Answers[$Questions[$i]['question_id']][$j]['answer'] . 'blah';

// or something simple like..

echo 'blah' . $Answers[$i]['answer'] . 'blah';
Fatal error: Cannot use string offset as an array in...
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

It appears to be. Gotta love their idiotic response..
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.

Thank you for your interest in PHP.

So don't do that.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Maybe $Answers was defined somewhere else. With so few information it's really hard to tell.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I'm saying that regardless of what that contains, or if it's not set, it should not cause a fatal error.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

astions wrote:I'm saying that regardless of what that contains, or if it's not set, it should not cause a fatal error.
It looks like the parser is attempting to convert the array into a string before reading the whole line of code. Does the problem disappear if you save the part of the array you're after in a normal variable, and then concatenate it? If so, then this is indeed a PHP bug.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

If I try to assign it to another var, or check it using isset(), I still get the fatal error.

This is PHP version 5.2.1
'./configure' '--enable-memcache' '--with-apxs2=/usr/local/apache/bin/apxs' '--with-mysqli=/usr/bin/mysql_config' '--with-mysql=/usr/bin/mysql_config' '--prefix=/usr/local/apache/php' '--with-config-file-path=/usr/local/apache/php' '--enable-force-cgi-redirect' '--disable-cgi' '--with-zlib' '--with-jpeg-dir=/usr/local/src/jpeg-6b/' '--with-png-dir=/usr' '--with-gd' '--with-curl=/usr' '--with-mcrypt=/usr'
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Whoa. That is ugly and unexpected. And this is only an issue when it comes to associative arrays?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Yeah, this code will reproduce the problem...

Code: Select all

<?php
$Answers = '';

$Questions[1]['question_id'] = 'test';

if (isset($Answers[$Questions[1]['question_id']][1]['answer']))
{
    echo $Answers[$Questions[1]['question_id']][1]['answer'];
}
But this will be ok..

Code: Select all

<?php
$Answers = array();

$Questions[1]['question_id'] = 'test';

if (isset($Answers[$Questions[1]['question_id']][1]['answer']))
{
    echo $Answers[$Questions[1]['question_id']][1]['answer'];
}
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

I think its because strings are numerically indexed, thats it.

Code: Select all

$string = 'test' ;

echo $string[0] ;  // this is ok

echo $string[6] ; // this returns a notice as that index is not initialized

isset($string[6]) ; // This is ok and no notice.  Index 6 is not initialized but its still possible

echo $string['someindex'] ; // This should throw an error.  You can never access a string via a a string index

isset($string['someindex']); // This should also throw an error because accessing a string via a string index is breaking the rules and there will never be a situation where isset() will return true if $string is of type string.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Based on what I read in the manual one is supposed to use {} to access string offsets, not [] which *should* be reserved only for arrays.

If PHP is allowing code to access string offsets with braces and then crashing because it can't do an array to string conversion I think this is an example of the language being a bit loose.

The more I look into this the more I think that this is clearly a bug. Accessing string offsets with braces should not be allowed.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If it is a bug it's by design
http://de2.php.net/string wrote:Note: They may also be accessed using braces like $str{42} for the same purpose. However, using square array-brackets is preferred because the {braces} style is deprecated as of PHP 6.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

volka wrote:If it is a bug it's by design
http://de2.php.net/string wrote:Note: They may also be accessed using braces like $str{42} for the same purpose. However, using square array-brackets is preferred because the {braces} style is deprecated as of PHP 6.
I don't agree with that at all. Brackets should strictly be reserved for array indexes and braces should be strictly reserved for string offsets. Using brackets for both is just plain silly as you won't know by looking at it whether it's an array or string offset.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

volka wrote:
http://de2.php.net/string wrote:Note: They may also be accessed using braces like $str{42} for the same purpose. However, using square array-brackets is preferred because the {braces} style is deprecated as of PHP 6.
Yes, I knew that but my opinion regarding to this would be:
astions wrote:I don't agree with that at all. Brackets should strictly be reserved for array indexes and braces should be strictly reserved for string offsets. Using brackets for both is just plain silly as you won't know by looking at it whether it's an array or string offset.
:wink:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

That doesn't make any sense? How the hell are you supposed to get a string offset off an array element?

Code: Select all

echo $array[1][1];
Have these people lost their mind?

So is this a string offset or a multidimensional array?

Code: Select all

echo $array[1][1];
There is code all over the place with lots of multidimensional arrays. These people are nuts. Who can I gripe to?
Post Reply