Monday, June 8, 2026
HomeLanguagesPHP | SplDoublyLinkedList setIteratorMode() Function

PHP | SplDoublyLinkedList setIteratorMode() Function

The SplDoublyLinkedList::setIteratorMode() function is an inbuilt function in PHP which is used to set the mode of iteration.

Syntax:

void SplDoublyLinkedList::setIteratorMode( int $mode )

Parameters: This function accepts single parameter $mode which holds two orthogonal sets of modes which are listed below:
The direction of the iteration are:

  • SplDoublyLinkedList::IT_MODE_LIFO (Stack style)
  • SplDoublyLinkedList::IT_MODE_FIFO (Queue style)

The behavior of the iterator are:

  • SplDoublyLinkedList::IT_MODE_DELETE (Elements are deleted by the iterator)
  • SplDoublyLinkedList::IT_MODE_KEEP (Elements are traversed by the iterator)

Return Value: This function does not return any value.

Below programs illustrate the SplDoublyLinkedList::setIteratorMode() function in PHP:
Program 1:




<?php
   
// Declare an empty SplDoublyLinkedList 
$list = new SplDoublyLinkedList();
   
// Add the element into SplDoublyLinkedList
$list->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO); 
   
// Use getIteratorMode() function
$mode = $list->getIteratorMode();
var_dump($mode);
   
// Add the element into SplDoublyLinkedList
$list->setIteratorMode(SplDoublyLinkedList::IT_MODE_DELETE); 
   
// Use getIteratorMode() function
$mode = $list->getIteratorMode();
var_dump($mode);
   
// Add the element into SplDoublyLinkedList
$list->setIteratorMode(SplDoublyLinkedList::IT_MODE_LIFO); 
   
// Use getIteratorMode() function
$mode = $list->getIteratorMode();
var_dump($mode);
   
?>


Output:

int(0)
int(1)
int(2)

Program 2:




<?php
   
// Declare an empty SplDoublyLinkedList 
$list = new SplDoublyLinkedList();
   
// Add the element into SplDoublyLinkedList
$list->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO
                    | SplDoublyLinkedList::IT_MODE_DELETE
                    | SplDoublyLinkedList::IT_MODE_LIFO); 
   
$mode = $list->getIteratorMode();
   
var_dump($mode & SplDoublyLinkedList::IT_MODE_FIFO);
                   
var_dump($mode & SplDoublyLinkedList::IT_MODE_LIFO);
                   
var_dump($mode & SplDoublyLinkedList::IT_MODE_DELETE);
                   
var_dump($mode & SplDoublyLinkedList::IT_MODE_KEEP); 
   
?>


Output:

int(0)
int(2)
int(1)
int(0)

Reference: https://www.php.net/manual/en/spldoublylinkedlist.setiteratormode.php

RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6895 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7018 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS