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
| ||||||||||||||||||||
comments |
6 Answers
activeoldestvotesup 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']);
| ||||
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.
| ||||||||
a comment |
up vote20down vote | You're trying to access a |
shareimprove this answer | edited Jan 15 at 9:03 | answered Feb 19 '15 at 15:21 Jon Surrell 2,20111829 |
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.
| ||
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.
| ||
add a comment |
up vote0down vote | Before to check the array, do this:if(!is_array($memcachedConfig)) $memcachedConfig = array();
| ||
add a comment |
相关文章推荐
- VSFTPD设置-允许root账户登录ftp
- 10.php桥接模式
- php 路由实现
- 手写PHP AJAX数据脚本
- PHP使用XHProf进行性能分析
- php连接 sql server
- PHP脚本的执行时间测试--pear与benchmark
- Debian 升级到 PHP 7,并支持并行安装
- PHP的数据类型
- PHP curl_setopt函数用法介绍上篇
- CI框架学习总结
- PHP之Phalcon支持(安装与配置)
- TP 学习之 session 方法的学习
- phpstorm使用教程
- FTPS VS SFTP
- PHP之XAMPP安装笔记
- 《PHP和MySQL Web开发》学习总结--文件系统和服务器的交互
- 详细解读PHP时区修改正确方法
- 9.php适配器模式
- php链接window的sqlserver