Home >Backend Development >PHP Tutorial >PHP警告Cannot use a scalar value as an array的解决方法_PHP

PHP警告Cannot use a scalar value as an array的解决方法_PHP

WBOY
WBOYOriginal
2016-06-01 12:13:361229browse

看到php的错误日志里有些这样的提示:

[27-Aug-2011 22:26:12] PHP Warning: Cannot use a scalar value as an array in /www/hx/enjoy.php on line 14
[27-Aug-2011 22:26:18] PHP Warning: Cannot use a scalar value as an array in /www/hx/enjoy.php on line 14

检查源程序,大概是下面这样子:
复制代码 代码如下:
$arr_hx = $mem->get('hx');
if(!$arr_hx) {
$arr_hx[‘a'] = 'b';
$mem->set('hx',$arr_hx);
}

基本明白了,在$mem->get没有得到值时返回的是false,此时$arr_hx是false,布尔值,然后又把它当成数组用了,导致产生了这样的提示。其实也是变量没定义的一种情况,在赋值前加一句 $arr_hx = array() 解决问题。

查了一下,有如下的说明:
引用
需要注意的是类型的转换:
如果一个变量名(如a)已经被定义为非数组类型,例如integer,那么a可以被转为floating point、string(甚至是object类型),但不可以是数组,即a[0]=1;是错误的,php会报出这样的警告“Cannot use a scalar value as an array“。即使a被定义为一维数组,也不能转为高维数组。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn