Home >Backend Development >PHP Tutorial >Solution to Cannot use a scalar value as an array under PHP_PHP Tutorial

Solution to Cannot use a scalar value as an array under PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:34:351443browse

Today when I was testing a php program, an error message appeared: Cannot use a scalar value as an array. This error message also appeared a few days ago. At that time, it seemed that it would be fine if I adjusted it a little. I didn't go into it deeply, but today it happened again. appeared.

I can’t fool around anymore, I have to find out the reasons and solutions, so I searched and searched online, but I couldn’t find the result after searching for a long time. It’s not that such problems can’t be found online, but there are very few. Someone gave a positive and accurate answer. The last paragraph of this article made me suddenly understand what was going on.

——————————————-
What needs to be noted is the type conversion:
If a variable name (such as a) has been defined as a non-array type, For example, integer, then a can be converted to floating point, string (even object type), but it cannot be an array, that is, a[0]=1; is wrong, and PHP will report such a 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 high-dimensional array.
——————————————-
The following are solutions to the problems found by other netizens:
After seeing this sentence, I carefully After checking the code, I found that a Boolean variable I had defined above was directly called as an array below, so an error occurred.

If a non-array element has been defined and assigned a value, and then used as an array, the error "Cannot use a scalar value as an array" will occur

For example: var $i=1000 ;

$i[5]=345; // An error will occur at this time,

So everyone should give up this non-standard way of writing code.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322349.htmlTechArticleWhen I was testing the php program today, an error message appeared: Cannot use a scalar value as an array, this The error message also appeared a few days ago. It seemed that it would be fine if I adjusted it a little...
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