Reads the first element in the deque without removing it.
The examples focus on the operation itself, including the important distinction: top reads the last element; bottom reads the first.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Syntax and Return Value
SplDoublyLinkedList::bottom()
The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.
Example 1: Basic Use
The list begins with 3 values. Notice how bottom changes or observes its indexed deque rather than an ordinary associative array.
<?php
$list = new SplDoublyLinkedList();
$list->push('tea');
$list->push('coffee');
$list->push('water');
echo json_encode($list->bottom());
Output from a successful local PHP 8.4 run:
"tea"
Example 2: Checking Behavior
The list begins with 4 values. Notice how bottom changes or observes its indexed deque rather than an ordinary associative array.
<?php
$list = new SplDoublyLinkedList();
$list->push('one');
$list->push('two');
$list->push('three');
$list->push('four');
echo json_encode($list->bottom());
Output from a successful local PHP 8.4 run:
"one"
Example 3: Practical Example
The list begins with 3 values. Notice how bottom changes or observes its indexed deque rather than an ordinary associative array.
<?php
$list = new SplDoublyLinkedList();
$list->push(4);
$list->push(8);
$list->push(12);
echo json_encode($list->bottom());
Output from a successful local PHP 8.4 run:
4
Important Behavior and Edge Cases
A missing element raises RuntimeException; isEmpty can guard the read.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| SplDoublyLinkedList::bottom | Reads the first element in the deque without removing it. |
| Related API or alternative | top reads the last element; bottom reads the first. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | A missing element raises RuntimeException; isEmpty can guard the read. |
| Confusing it with a related API | top reads the last element; bottom reads the first. |
Key Takeaways
- Reads the first element in the deque without removing it.
- A missing element raises RuntimeException; isEmpty can guard the read.
- top reads the last element; bottom reads the first.
Use SplDoublyLinkedList::bottom when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.