Solution to cannot use a scalar value as an array in PHP

Source: Internet
Author: User

Testing PHP today ProgramThe error message "cannot use a scalar value as an array" is displayed. The error message returned a few days ago. At that time, it seemed to be fine-tuned and I didn't go into details, today, it appears again.

I can't confuse myself any more. I have to find out the cause and solution, so I went online to search for it. I haven't found any results for a long time. It's not that I can't find such a problem on the Internet, but few people make positive and accurate answers. Last articleArticleA piece of text made me suddenly understand what happened.

--------------
Note the following type conversion:
If a variable name (such as a) has been defined as a non-array type, such as integer, A can be converted to floating point, string (or even object type), but cannot be an array, that is, a [0] = 1; is incorrect. php will report the warning "cannot use a scalar value as an array". Even if a is defined as a one-dimensional array, it cannot be converted into a high-dimensional array.
--------------
The solutions to the problems found by other netizens are as follows:
After seeing this sentence, I checked it carefully.CodeIt is found that a Boolean variable that I have defined above is called directly as an array at the bottom, so an error occurs.

If you have defined a non-array element and assigned a value, and used it as an array, the cannot use a scalar value as an Array Error will occur.

For example, VAR $ I = 1000;

$ I [5] = 345; // an error occurs at this time,

Therefore, we should give up this nonstandard code writing method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.