PHP prev() function with example

PHP prev() function: Here, we will find out about the prev() function with example in PHP.

PHP prev() function

prev() function right off the bat moves the present pointer to the past component and returns the component.

Syntax:

prev(array); 

Note: As we have talked about in last two posts (current() function in PHP and next() function in PHP) that a cluster has a pointer that focuses to the primary component as a matter of course, next() function can be utilized to move the pointer to next component and afterwards to print it and also a prev() function can be utilized to move the pointer to past component and print it.

Example:

    Input:
    $arr1 = array(10, 20, 30, 40, 50);
    Function call:
    current($arr1);
    next($arr1);
    prev($arr1);
    Output:
    10
    20
    10


    Input:
    $arr2 = array("name" => "Kishan", "age" => 21, "Gender" => "Male");
    Function call:
    current($arr2);
    next($arr2);
    prev($arr2);
    Output:
    Kishan
    21
    Kishan

PHP Code:

<?php
    $arr1 = array(10, 20, 30, 40, 50);
    $arr2 = array("name" => "Kishan", "age" => 21, "Gender" => "Male");
    
    echo current($arr1) . "\n"; //print first element
    echo next($arr1) . "\n"; //move to next element and print 
    echo prev($arr1) . "\n"; //move to previous element and print 

    echo current($arr2) . "\n"; //print first element
    echo next($arr2) . "\n"; //move to next element and print 
    echo prev($arr2) . "\n"; //move to previous element and print     
?>

Output

10
20
10
Kishan
21
Kishan

Leave a Comment

error: Alert: Content is protected!!