Current() and Key() are the useful functions. PHP uses a pointer to keep track of which element being used by the script. We can use Current() and Keys() function to find out the value and index respectively.
See the following example that demonstrate how current() and key() functions work. When we open first a PHP script with the following code in it, you can check which index PHP first looks at first:
- $Capitals[7] = "New Delhi";
- $Capitals[2] = "Washington";
- $Capitals[12] = "Landon";
- $current_IndexValue = key($Capitals);
- echo ( $current_IndexValue );
- $current_Value = Current($Capitals);
- echo ( $current_Value);
from the above the key() function returns the value of 7 because it returns the index value of the first element you placed into the array. And if we use current() function then it return “New Delhi”.
Now one thing next that if you add new element in the above array like as:
- $Capitals[] = "Berlin";
Php set this newly added element at the index 13 not at 0, because the lowest value of the index being used is 12, and the next unfilled element is numbered 13.