PHP warning: illegal string offset
P粉548512637
P粉548512637 2023-10-09 23:52:16
0
2
624

After updating my php version to 5.4.0-3, I'm getting a strange PHP error.

I have this array:

Array
(
    [host] => 127.0.0.1
    [port] => 11211
)

I get weird warnings when I try to access it like this

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 reset the error level.

P粉548512637
P粉548512637

reply all(2)
P粉420868294

ErrorIllegal string offset 'whatever' in...Usually means: You are trying to use a string as a complete array.

This is actually possible because in php strings can be treated as arrays of single characters. So you think $var is an array with keys, 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 the actual effect here: http://ideone.com/fMhmkR

For those of you asking this question and trying to turn false ambiguity into a solution, like me.

P粉267885948

Please try this way...I have tested this code...it works...

$memcachedConfig = array("host" => "127.0.0.1","port" => "11211");
print_r($memcachedConfig['host']);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template