search

Home  >  Q&A  >  body text

PHP warning: illegal string offset

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粉548512637P粉548512637431 days ago637

reply all(2)I'll reply

  • P粉420868294

    P粉4208682942023-10-10 12:23:40

    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.

    reply
    0
  • P粉267885948

    P粉2678859482023-10-10 10:42:00

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

    $memcachedConfig = array("host" => "127.0.0.1","port" => "11211");
    print_r($memcachedConfig['host']);

    reply
    0
  • Cancelreply