Page 1 of 2
Fatal Error - Cannot use string offset as an array...
Posted: Tue Jul 24, 2007 8:55 am
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...
Posted: Tue Jul 24, 2007 9:24 am
by Oren
Posted: Tue Jul 24, 2007 9:55 am
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.
Posted: Tue Jul 24, 2007 11:46 am
by Oren
Maybe $Answers was defined somewhere else. With so few information it's really hard to tell.
Posted: Tue Jul 24, 2007 12:11 pm
by Benjamin
I'm saying that regardless of what that contains, or if it's not set, it should not cause a fatal error.
Posted: Tue Jul 24, 2007 12:39 pm
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.
Posted: Tue Jul 24, 2007 12:54 pm
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'
Posted: Tue Jul 24, 2007 12:59 pm
by superdezign
Whoa. That is ugly and unexpected. And this is only an issue when it comes to associative arrays?
Posted: Tue Jul 24, 2007 1:04 pm
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'];
}
Posted: Tue Jul 24, 2007 3:58 pm
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.
Posted: Tue Jul 24, 2007 4:37 pm
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.
Posted: Tue Jul 24, 2007 5:33 pm
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.
Posted: Tue Jul 24, 2007 5:43 pm
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.
Posted: Tue Jul 24, 2007 6:45 pm
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.

Posted: Tue Jul 24, 2007 7:16 pm
by Benjamin
That doesn't make any sense? How the hell are you supposed to get a string offset off an array element?
Have these people lost their mind?
So is this a string offset or a multidimensional array?
There is code all over the place with lots of multidimensional arrays. These people are nuts. Who can I gripe to?