PHP pos() Function
The PHP pos() function returns the current element of the array. Every array in PHP has an internal pointer to its "current" element, which is initialized to the first element inserted into the array.
Please note that this function is alias of current() function.
Syntax
pos(array)
Parameters
array |
Required. Specify the input array. |
Return Value
Returns the value of the array element which is currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, it returns false.
Note: This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Therefore, use === operator for testing the return value of this function.
Exceptions
NA.
Example:
The example below shows the usage of pos() function.
<?php $Arr = array(10, 20, "Hello", "World"); echo pos($Arr)."\n"; //prints 10 echo next($Arr)."\n"; //prints 20 echo pos($Arr)."\n"; //prints 20 echo end($Arr)."\n"; //prints World echo pos($Arr)."\n"; //prints World echo "\n"; $Arr1 = array(); var_dump(pos($Arr1)); //bool(false) $Arr2 = array(array()); var_dump(pos($Arr2)); //array(0) { } ?>
The output of the above code will be:
10 20 20 World World bool(false) array(0) { }
Example:
Consider one more example where the pos() function is used with an associative array.
<?php $Arr = array(10=>"Red", 20=>"Green", 30=>"Blue", 40=>"Black", 50=>"White"); echo pos($Arr)."\n"; //prints Red echo next($Arr)."\n"; //prints Green echo pos($Arr)."\n"; //prints Green echo end($Arr)."\n"; //prints White echo pos($Arr)."\n"; //prints White ?>
The output of the above code will be:
Red Green Green White White
❮ PHP Array Reference