Index
In PHP, an array index refers to the location of a value within an array. The index is used to identify the position of an element within the array.
In PHP, arrays are zero-indexed, which means that the first element of the array has an index of 0. For example, if you have an array called $fruits, the first element of the array would be $fruits[0]. The second element would be $fruits[1], and so on.
Here is an example of an array in PHP with three elements:
$fruits = array("apple", "banana", "cherry");
echo $fruits[0]; // Outputs "apple"
echo $fruits[1]; // Outputs "banana"
echo $fruits[2]; // Outputs "cherry"
You can also use negative indices to access elements of the array, starting from the end. For example, $fruits[-1] would refer to the last element of the array, $fruits[-2] would refer to the second to last element, and so on.
$fruits = array("apple", "banana", "cherry");
echo $fruits[-1]; // Outputs "cherry"
echo $fruits[-2]; // Outputs "banana"
echo $fruits[-3]; // Outputs "apple"
You can also use numeric indices that are larger than the size of the array. In this case, PHP will create empty elements in the array to fill the gap between the end of the array and the specified index.
$fruits = array("apple", "banana", "cherry");
$fruits[5] = "grapes";
print_r($fruits);
// Outputs: Array ( [0] => apple [1] => banana [2] => cherry [5] => grapes )