Solution to the problem that Cannot use a scalar value as an array under PHP

  • 2020-03-31 21:00:31
  • OfStack

When I was testing the PHP program today, I got an error: "Cannot use a scalar value as an array". This error was also given a few days ago, which seemed to be fine with a slight adjustment at that time.

Can't fool around, have to find out the causes and solutions, so I went to the Internet to search for ah, for a long time did not find the results, not the Internet can not find such questions, but very few people do positive accurate answers. A passage from the last article made it clear to me at once.

-- -- -- -- -- -- -- -
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), but not to an array, i.e., a[0]=1; PHP will issue the warning "Cannot use a scalar value as an array". Even if a is defined as a one-dimensional array, it cannot be converted to a higher-dimensional array.
-- -- -- -- -- -- -- -
Here's how to solve some of the problems others have found:
After reading this, I went through the code and found that one of the Boolean variables I had defined above was called directly as an array below, so there was an error.

If a non-array element is already defined and assigned, using it as an array will result in an error that Cannot use a scalar value as an array

Such as: var $I = 1000;

$I [5] = 345; // something will go wrong,

So you have to give up this non-standard way of writing code.

Related articles: