PHP警告Cannot use a scalar value as an array的解决方法_php技巧
May 17, 2016 am 09:13 AM看到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被定义为一维数组,也不能转为高维数组。

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey
