Checks whether a specified numeric list offset is present.
The examples focus on the operation itself, including the important distinction: offsetGet fetches an existing value; offsetExists checks availability 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::offsetExists($index)
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.
Parameters
- index: required.
Example 1: Basic Use
The list begins with 3 values. Notice how offsetExists 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 $list->offsetExists(1) ? "present" : "missing";
Output from a successful local PHP 8.4 run:
present
Example 2: Checking Behavior
The list begins with 4 values. Notice how offsetExists 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 $list->offsetExists(1) ? "present" : "missing";
Output from a successful local PHP 8.4 run:
present
Example 3: Practical Example
The list begins with 3 values. Notice how offsetExists 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 $list->offsetExists(2) ? "present" : "missing";
Output from a successful local PHP 8.4 run:
present
Important Behavior and Edge Cases
Offsets are not associative keys; use numeric positions, not arbitrary strings.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| SplDoublyLinkedList::offsetExists | Checks whether a specified numeric list offset is present. |
| Related API or alternative | offsetGet fetches an existing value; offsetExists checks availability first. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Offsets are not associative keys; use numeric positions, not arbitrary strings. |
| Confusing it with a related API | offsetGet fetches an existing value; offsetExists checks availability first. |
Key Takeaways
- Checks whether a specified numeric list offset is present.
- Offsets are not associative keys; use numeric positions, not arbitrary strings.
- offsetGet fetches an existing value; offsetExists checks availability first.
Use SplDoublyLinkedList::offsetExists when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.