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技巧

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被定义为一维数组,也不能转为高维数组。
Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

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

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

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

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles