Open In App
Related Articles

PHP | Ds\Deque last() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
The Ds\Deque::last() function is an inbuilt function in PHP which is used to return the last element of Deque if Deque is not empty. Syntax:
public Ds\Deque::last( void ) : mixed
Parameters: This function does not accept any parameter. Return Value: This function returns the last element in the deque if it is not empty. Exception: This function throws UnderflowException if the Deque is empty. Below programs illustrate the Ds\Deque::last() function in PHP: Program 1:
<?php
  
// Create a Deque
$deck = new \Ds\Deque([1, 2, 3, 4, 5, 6]);
  
echo("Elements in the deque\n");
  
// Display the Deque Elements
var_dump($deck);
  
echo("\nLast element in the deque: ");
  
// Use last() function to display the 
// last element from the deque
var_dump($deck->last());
  
?>

                    
Output:
Elements in the deque
object(Ds\Deque)#1 (6) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
}

Last element in the deque: int(6)
Program 2:
<?php
  
// Create a Deque
$deck = new \Ds\Deque(["geeks", "for", "geeks"]);
  
echo("Elements in the deque\n");
  
// Display the Deque Elements
print_r($deck);
  
echo("\nLast element in the deque: ");
  
// Use last() function to display the 
// last element from the deque
print_r($deck->last());
  
?>

                    
Output:
Elements in the deque
Ds\Deque Object
(
    [0] => geeks
    [1] => for
    [2] => geeks
)

Last element in the deque: geeks
Reference: http://php.net/manual/en/ds-deque.last.php

Last Updated : 14 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads