A Conjecture About the array model in PHP (by misko lee)

Source: Internet
Author: User

According to my personal understanding, the array should be a contiguous space in the memory. Before using the array, you need to open up a blank memory of the specified size in the memory. If the declared size is smaller than the available continuous memory size, the error of memory overflow should be reported.
For children's shoes who have used PHP arrays, they should be able to quickly determine that the arrays in PHP do not have the above features. 1. the array declaration does not need to be determined by the size. 2. There is no upper limit for the array. Through these two points, we can determine that the arrays in PHP are a loose structure of the linked list, rather than continuous memory space.
Arrays in PHP can be divided into index arrays and associated arrays. An index array is a counting array structure. The associated array is an implementation of the map data structure, that is, the key-value structure.
The following examples are used to discuss the index array and associated array.
$ Arr = array (, 3, 'name' => 'misko _ lee ', 'age' => 22); // define an index and associate a Hybrid array
For ($ I = 0; $ I <count ($ arr); $ I ++)
Echo $ arr [$ I]; // here we can see the error message of subscript overflow.

The Cout ($ arr) function call returns the length of the $ arr array. However, the for loop can only output index arrays normally. Therefore, we can determine that the index array and the associated array are two different implementations. Therefore, it is proved that the arrays in PHP are loosely structured.
We also have the following conjecture about the index array:
$ Arr [100] = 100;
$ Arr [] = 101; // at this time, $ arr [] automatically increases the subscript by 101.

The above experiment proves our conjecture that the array counter in PHP does not count from the first address of the Array Memory, but is a pseudo counting method. Therefore, it is incorrect to use the counter to determine the array size.
The above comments are not reliable because they are gibberish. Students are welcome to make mistakes. Good people are safe for life.
Last PS sentence: If the array uses foreach for traversal, use the reset () function to reset the array pointer for traversal.

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.