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:
PHP
PHP
public Ds\Deque::last( void ) : mixedParameters: 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:
Program 2:
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)
<?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:
Reference: https://www.php.net/manual/en/ds-deque.last.php
Elements in the deque
Ds\Deque Object
(
[0] => geeks
[1] => for
[2] => geeks
)
Last element in the deque: geeks