Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

php - 非法字符串偏移量警告PHP(Illegal string offset Warning PHP)

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

(将我的php版本更新到5.4.0-3后,我收到一个奇怪的PHP错误。)

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.

(我真的不想只编辑我的php.ini并重新设置错误级别。)

  ask by thesonix translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error Illegal string offset 'whatever' in... generally means: you're trying to use a string as a full array.

(错误Illegal string offset 'whatever' in...通常意味着:您正在尝试将字符串用作完整数组。)

That is actually possible since strings are able to be treated as arrays of single characters in php.

(这实际上是可能的,因为在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:

(因此,您认为$ var是带有键的数组,但是它只是具有标准数字键的字符串 ,例如:)

$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

(您可以在这里看到实际的效果: 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.

(对于那些遇到此问题的人,就像我一样,试图将错误的模糊性转化为解决该问题的方法。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...