PHP warning Cannot use a scalar value as an array solution

  • 2020-05-12 02:18:15
  • OfStack

You can see some hints in php's 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, which looks something like this:
 
$arr_hx = $mem->get('hx'); 
if(!$arr_hx) { 
$arr_hx[ ' a'] = 'b'; 
$mem->set('hx',$arr_hx); 
} 

Basically understood, at $mem- > get returns false when it doesn't get a value, and $arr_hx is false, a Boolean, and then USES it as an array again, resulting in this prompt. If the variable is not defined, add $arr_hx = array() before the assignment to solve the problem.

After checking 1, there are the following instructions:
reference
Note the type conversion:
If a variable name (such as a) is already defined as a non-array type, such as integer, then a can be converted to floating point, string (or even object type), but not to an array, a[0]=1; Is wrong, php will report the warning "Cannot use a scalar value as an array". Even if a is defined as a 1-dimensional array, it cannot be converted to a higher-dimensional array.

Related articles: