您的位置:首页 > 编程语言 > PHP开发

Illegal string offset Warning PHP

2016-01-20 16:15 921 查看


Illegal
string offset Warning PHP






up
vote62down
votefavorite
11

I get a strange PHP error after updating my php version to 5.4.0-3.

I have this array:
Array
(
[host] => 127.0.0.1
[port] => 11211
)


When I try to access it like this I get strange warnings
print $memcachedConfig['host'];
print $memcachedConfig['port'];

Warning: Illegal string offset 'host' in ....
Warning: Illegal string offset 'port' in ...


I really don't want to just edit my php.ini and re-set the error level.

Thanks for any help!

php warnings
shareimprove
this question
edited Jun
24 '12 at 9:02





hakre
124k22181342

asked Mar 26 '12 at 8:55





thesonix
6801712

 
11 
Obviously 
$memcachedConfig
 is
not that array. Show 
var_dump($memcachedConfig);
 – zerkms Mar
26 '12 at 8:56 
 
just a guess, but have you tried using actual strings as keys? I mean, 
['host']
=> '127.0.0.1'
 – maialithar Mar
26 '12 at 8:57 
 
Shouldn't it be 
['host']
=> '127.0.0.1'
 etc. ? – luukes Mar
26 '12 at 8:58
1 
It means the keys does not exist. Check your variable with 
var_export($memcachedConfig)
 just
before the "print". – Skrol29 Mar
26 '12 at 8:59 
1 
What most people missed is this doesn't exactly mean the index doesn't exist -- that produces "undefined index" message.
This is a different error. – grantwparks Jul
19 '14 at 6:24
show 3 more
comments


6 Answers

activeoldestvotes

up vote18down
voteaccepted
Please try this way.... I have tested this code.... It works....
$memcachedConfig = array("host" => "127.0.0.1","port" => "11211");
print_r ($memcachedConfig['host']);


shareimprove
this answer
edited Jun
28 '12 at 11:41





Emir Akaydın
4,26911643

answered Mar 26 '12 at 9:00





letsnurture
1,208918

 
 
Found it. Thanks for your help. var_dump helped. I loaded the array from a config file, which had the strage content
like this. array(2) { ["host"]=> string(9) "127.0.0.1" ["port"]=> string(5) "11211" } string(5) "m_prefix" PHP 5.4 now $xx['host'] threw the warning correctly. – thesonix Mar
26 '12 at 9:17
add
a comment
up vote57down
vote
The error 
Illegal
string offset 'whatever' in...
 generally means: you're trying to use a string as a full array.

That is actually possible since strings are able to be treated as arrays of single characters in php. So you're thinking the $var is an array with a key, but it's just a string with standard numeric keys,
for example:
$fruit_counts = array('apples'=>2, 'oranges'=>5, 'pears'=>0);
echo $fruit_counts['oranges']; // echoes 5
$fruit_counts = "an unexpected string assignment";
echo $fruit_counts['oranges']; // causes illegal string offset error


You can see this in action here: http://ideone.com/fMhmkR

For those who come to this question trying to translate the vagueness of the error into something to do about it, as I was.

shareimprove
this answer
edited Aug
20 '14 at 16:55

answered Nov 28 '13 at 17:06





Kzqai
9,832127097

 
2 
It's good to know this! – alexcristea Mar
10 '14 at 7:41
3 
I bet it can be shown this was the reason the original problem happened. Most comments incorrectly assume "undefined
index" was the error. – grantwparks Jul
19 '14 at 6:28
add
a comment
up vote20down
vote


TL;DR

You're trying to access a 
string
 as
if it were an array, with a key that's a 
string
string
 will
not understand that. In code we can see the problem:
"hello"["hello"];
// PHP Warning:  Illegal string offset 'hello' in php shell code on line 1

"hello"[0];
// No errors.

array("hello" => "val")["hello"];
// No errors. This is *probably* what you wanted.


In depth


Let's see that error:

Warning: Illegal string offset 'port' in ...

What does it say? It says we're trying to use the string 
'port'
 as
an offset for a string. Like this:
$a_string = "string";

// This is ok:
echo $a_string[0]; // s
echo $a_string[1]; // t
echo $a_string[2]; // r
// ...

// !! Not good:
echo $a_string['port'];
// !! Warning: Illegal string offset 'port' in ...


What causes this?

For some reason you expected an 
array
,
but you have a 
string
.
Just a mix-up. Maybe your variable was changed, maybe it never was an 
array
,
it's really not important.


What can be done?

If we know we should have an 
array
,
we should do some basic debugging to determine why we don't have an 
array
.
If we don't know if we'll have an 
array
 or 
string
,
things become a bit trickier.

What we can do is all sorts of checking to ensure we don't have notices, warnings or errors with things like 
is_array
 and 
isset
 or 
array_key_exists
:
$a_string = "string";
$an_array = array('port' => 'the_port');

if (is_array($a_string) && isset($a_string['port'])) {
// No problem, we'll never get here.
echo $a_string['port'];
}

if (is_array($an_array) && isset($an_array['port'])) {
// Ok!
echo $an_array['port']; // the_port
}

if (is_array($an_array) && isset($an_array['unset_key'])) {
// No problem again, we won't enter.
echo $an_array['unset_key'];
}

// Similar, but with array_key_exists
if (is_array($an_array) && array_key_exists('port', $an_array)) {
// Ok!
echo $an_array['port']; // the_port
}


There are some subtle differences between 
isset
 and 
array_key_exists
.
For example, if the value of 
$array['key']
 is 
null
isset
 returns 
false
array_key_exists
 will
just check that, well, the key exists.

shareimprove
this answer
edited Jan
15 at 9:03

answered Feb 19 '15 at 15:21





Jon Surrell
2,20111829

 add
a comment
up vote1down
vote
In my case i change mysql_fetch_assoc to mysql_fetch_array and solve. It takes 3 days to solve :-( and the other versions of my proyect run with fetch assoc.

shareimprove
this answer
answered Jun 5 '15 at 18:56





Pichitron
411

 add
a comment
up vote0down
vote
Just to expand on this a little. Same error, different issue.

I was simply checking against a value that didn't exist as I had a mixture of single and multidimensional arrays within a larger array. So, when I checked against $array['key'] it threw an 'Illegal String' error on the items that had no associated ['key'].
Just for reference.

shareimprove
this answer
answered Sep 8 '14 at 13:54





rob_was_taken
947

 add
a comment
up vote0down
vote
Before to check the array, do this:
if(!is_array($memcachedConfig))
$memcachedConfig = array();


shareimprove
this answer
answered Dec 4 '14 at 11:14





dlopezgonzalez
1,67011222

 add
a comment
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: