phparrays

Does the internal index of a PHP array automatically increase, if I create an array element with []?


I need to understand through the PHP official documentation if in this case:

$vett[]=8.0;
$vett[]=6.5;

the internal index is increased automatically (to point to the next free location) soon after the end of the first statement or at the beginning of the second statement.


The confusion arises from the book of Robert Nixon:

enter image description here


Solution

  • First of all, internal array pointer means something absolutely different. It's a thing related to some outdated method for iterating over existing arrays (which is almost never used nowadays).

    What you are talking about could be called "internal index", but there is no such thing in PHP.

    The index is just set automatically at the beginning of each statement.

    The PHP manual explains it pretty straightforward:

    if no key is specified, the maximum of the existing int indices is taken, and the new key will be that maximum value plus 1 (but at least 0). If no int indices exist yet, the key will be 0 (zero).

    So,

    everything is done right on the assignment, there is no "internal index" to be kept. Just a maximum index from the array. Check this code:

    $vett = [];
    $vett[60]=8.0;
    $vett[]=6.5;
    var_dump($vett);
    

    It's as simple as max index+1 right on the assignment.