PHP 8.3 brings changes to the empty array behavior regarding negative indices. Since PHP 8.3, assigning a negative index n
to an empty array now makes the next index n + 1
instead of 0
.
Before PHP 8.3, when working with an empty array, adding an item with a negative index and then adding another item will always start the second item at index 0.
<?php
$array = [];
$array[-3] = 'A';
$array[] = 'B';
print_r($array); // Array([-3] => A, [0] => B)
Since PHP 8.3, adding an item to an empty array with a negative index, followed by adding another item, now the second item will be positioned at the subsequent negative index rather than starting at 0.
<?php
$array = [];
$array[-3] = 'A';
$array[] = 'B';
print_r($array); // Array([-3] => A, [-2] => B)
This change ensures more predictable and consistent handling of negative indices.
Leave a Comment
Cancel reply