PHP pos() function
Example
Output the value of the current element in the array:
<?php $people = array("Bill", "Steve", "Mark", "David"); echo pos($people) . "<br>"; ?>
Definition and usage
The pos() function returns the value of the current element in the array.
This function is current() Function alias.
Each array has an internal pointer that points to its 'current' element, initially pointing to the first element inserted into the array.
Tip:This function does not move the internal pointer of the array.
Related methods:
- current() - Return the value of the current element in the array
- end() - Move the internal pointer to the last element in the array and output
- next() - Move the internal pointer to the next element in the array and output
- prev() - Move the internal pointer to the previous element in the array and output
- reset() - Move the internal pointer to the first element in the array and output
- each() - Return the key name and value of the current element, and move the internal pointer forward
Syntax
pos(array)
Parameter | Description |
---|---|
array | Required. Specifies the array to be used. |
Technical details
Return value: | Return the value of the current element in the array, if the current element is empty or the current element has no value, return FALSE. |
PHP Versie: | 4+ |
Meer voorbeelden
Voorbeeld 1
Demonstreer alle relevante methoden:
<?php $people = array("Bill", "Steve", "Mark", "David"); echo current($people) . "<br>"; // Het huidige element is Bill echo next($people) . "<br>"; // De volgende element van Bill is Steve echo current($people) . "<br>"; // De huidige huidige element is Steve echo prev($people) . "<br>"; // De vorige element van Steve is Bill echo end($people) . "<br>"; // De laatste element is David echo prev($people) . "<br>"; // De element voor David is Mark echo current($people) . "<br>"; // Het huidige huidige element is Mark echo reset($people) . "<br>"; // Verplaatst de interne pointer naar het eerste element van de array, dat Bill is echo next($people) . "<br>"; // De volgende element van Bill is Steve print_r (each($people)); // Keert de huidige elementnaam en -waarde terug (momenteel is dit Steve) en verplaatst de interne pointer ?>