My question is exactly as stated above.
I’m wondering if it is enough to “trust” that the internal pointer of the array will always point to its first element no matter what and simply use this:
$bar = current($foo);
Or if I should take no chances and first reset the internal pointer of the array to its first element before using it like so:
reset($foo);
$bar = current($foo);
The reason I ask is because if the current() function alone is not reliable it could potentiality present misleading information to the end-user and I’ll prefer to avoid any emails with the following subject:
"What is this? I don't even..."
I’m sure you understand. 🙂
EDIT:
I understand that the point of the current() function is to access wherever the current internal pointer of the array is. My question is whether or not the internal pointer is guaranteed to be pointing at the first element of the array immediately after array creation when no other function calls should have moved the internal pointer.
The point of
current()is to access the element at wherever the internal pointer of the array currently is. If you want to use a function that always returns the first element, eitherreset()the internal pointer of that array (it also returns the value in the first element so you don’t need to callcurrent()after that), or use$foo[0](doesn’t move the pointer, use only for properly-sorted, numerically-indexed arrays).With said, to answer your question,
current()is guaranteed to return the first element of an array immediately after you create it using thearray(...)notation. From the manual example for thecurrent()function:And from the manual example for the
reset()function: