Content · Web Development · PHP

PHP SplDoublyLinkedList::pop() Method: Usage and Examples

Removes and returns the final element of the list. Calling pop on an empty list raises RuntimeException.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Removes and returns the final element of the list.

The examples focus on the operation itself, including the important distinction: shift removes the first element rather than the final one.

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::pop()

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 pop 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->pop());
echo " remaining=" . count($list);

Output from a successful local PHP 8.4 run:

"water" remaining=2

Example 2: Checking Behavior


The list begins with 4 values. Notice how pop 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->pop());
echo " remaining=" . count($list);

Output from a successful local PHP 8.4 run:

"four" remaining=3

Example 3: Practical Example


The list begins with 3 values. Notice how pop 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->pop());
echo " remaining=" . count($list);

Output from a successful local PHP 8.4 run:

12 remaining=2

Important Behavior and Edge Cases


Calling pop on an empty list raises RuntimeException.

Comparison with Related Approaches


ApproachDifference
SplDoublyLinkedList::popRemoves and returns the final element of the list.
Related API or alternativeshift removes the first element rather than the final one.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsCalling pop on an empty list raises RuntimeException.
Confusing it with a related APIshift removes the first element rather than the final one.

Key Takeaways


  • Removes and returns the final element of the list.
  • Calling pop on an empty list raises RuntimeException.
  • shift removes the first element rather than the final one.

Use SplDoublyLinkedList::pop when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.