Open In App

PHP pos() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The pos() is an inbuilt function in PHP which is used to return the value of the element in an array which the internal pointer is currently pointing to. The pos() function does not increment or decrement the internal pointer after returning the value. In PHP all arrays have an internal pointer. This internal pointer points to some element in that array which is called as the current element of the array. Usually, the current element is the first inserted element in the array.

Syntax:

pos($array)

Parameters: The pos() function accepts a single parameter $array. It is the array of which we want to find the current element.

Return Value: It returns the value of the element in the array which the internal pointer is currently pointing to. If the array is empty then the pos() function returns FALSE.

Below programs illustrate the pos() function in PHP:

Program 1:




<?php
    // input array
    $arr = array("Ram", "Shyam", "Geeta", "Rita");
      
    // Here pos() function returns current element.
    echo pos($arr)."\n"
      
    // next() function increments internal pointer 
    // to point to next element i.e, Shyam.
    echo next($arr)."\n";
      
    // Printing current position element.
    echo pos($arr)."\n"
      
    // Printing previous element of the current one.
    echo prev($arr)."\n"
      
    // Printing end element of the array
    echo end($arr)."\n"
      
    // Printing current position element.
    echo pos($arr)."\n"
?>


Output:

Ram
Shyam
Shyam
Ram
Rita
Rita

Program 2:




<?php
    // input array
    $arr = array("P", "6", "M", "F");
      
    // Here pos() function returns current element.
    echo pos($arr)."\n"
      
    // next() function increments internal pointer 
    // to point to next element i.e, 6.
    echo next($arr)."\n";
      
    // Printing current position element.
    echo pos($arr)."\n"
      
    // Printing previous element of the current one.
    echo prev($arr)."\n"
      
    // Printing end element of the array
    echo end($arr)."\n"
      
    // Printing current position element.
    echo pos($arr)."\n"
?>


Output:

P
6
6
P
F
F

Reference:
http://php.net/manual/en/function.pos.php



Last Updated : 20 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads