Home  >  Article  >  Backend Development  >  scalar PHP warning Cannot use a scalar value as an array solution

scalar PHP warning Cannot use a scalar value as an array solution

WBOY
WBOYOriginal
2016-07-29 08:47:391322browse

I saw some prompts like this in the php error log:
[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
Check the source program, it probably looks like this:

Copy Code The code is as follows:


$arr_hx = $mem->get('hx');
if(!$arr_hx) {
$arr_hx['a'] = 'b';
$mem-> ;set('hx',$arr_hx);
}


Basically understood, when $mem->get does not get the value, it returns false. At this time, $arr_hx is false, a Boolean value, and then it is returned Used as an array, resulting in such a prompt. In fact, it is also a case where the variable is not defined. Add $arr_hx = array() before the assignment to solve the problem.
After checking, there is the following explanation:
Quote
What you need to pay attention to is the type conversion:
If a variable name (such as a) has been defined as a non-array type, such as integer, then a can be converted to a floating point, string (even object type), but not an array, that is, a[0]=1; is wrong, and PHP will issue a warning like "Cannot use a scalar value as an array". Even if a is defined as a one-dimensional array, it cannot be converted to a high-dimensional array.

The above introduces the solution to the scalar PHP warning Cannot use a scalar value as an array, including scalar content. I hope it will be helpful to friends who are interested in PHP tutorials.

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