John Davidson

Illegal string offset Warning PHP

0 comments
Message:


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.



My Answer:
To fix this issue, you can modify your code to check if the key exists in the array before trying to access it. Here's an example:

php
if (isset($memcachedConfig['host'])) {
print $memcachedConfig['host'];
}

if (isset($memcachedConfig['port'])) {
print $memcachedConfig['port'];
}


This way, you can avoid the "Illegal string offset" warning by only accessing the keys that actually exist in the array.

Rate this post

3 of 5 based on 8421 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us