1-point conjecture about the array model in PHP (by Misko Lee)

Source: Internet
Author: User
A little conjecture about the array model in PHP (by Misko Lee)

according to my own understanding, the array should be a contiguous space in memory. It is necessary to open a blank memory in memory before the array is used, and if the declared size is less than the available contiguous memory size, a memory overflow error should be reported.
for children who have used a php Array, it should be able to quickly determine that the array in PHP does not have the above characteristics. One, the array declaration does not require a size determination ; second, the array does not use the upper limit. With these two points, we can tell that the array in PHP is a loosely structured form of a list, not a contiguous memory space.
Arrays in PHP can be divided into indexed arrays and associative arrays of two forms. An indexed array is a count-type array structure. And the associative array is the map data Structure An implementation, that is, the structure of Key-value.
We have some discussions on indexed arrays and associative arrays using the following example.
$arr =array ("the name ' = ' Misko_lee ', ' age ' =>22);// define an index, associative mixed-type array
For ($i =0; $i <> < span=""> <>
echo $arr [$i];// Here we can see an error indicating the subscript overflow

the Cout ($arr) function call returns The length of the $arr array. However , only the for loop can output an indexed array normally. Thus, we can judge, indexed arrays and associative arrays are two different implementations. It also proves that the array in PHP is a loosely-structured conjecture.
about indexed arrays We also have the following assumptions:
$arr [100]=100;
$arr []=101;// at this time $arr [] Auto-growth subscript is 101.

The above experiment proves our conjecture thatthe array counters in PHP are not counted from the first address of the array memory .It is a pseudo-counting method. Therefore, the use of counters to determine the size of the array is a complete error.
the above remarks are personal nonsense, with very unreliable factors. Welcome all Seniors Shoes pointed out the mistake. A good man is safe in life.
the last PS sentence: If the array uses a foreach traversal if it is not the reason for demand, use the reset () function to traverse the array pointer to reset

  • Related Article

    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.