Tuesday, June 9, 2026
HomeLanguagesPHP SplDoublyLinkedList add() Function

PHP SplDoublyLinkedList add() Function

The SplDoublyLinkedList::add() function is an inbuilt function in PHP which is used to add a new value at the given index.

Syntax:

void SplDoublyLinkedList::add( $index, $newval )

Parameters: It contains two parameters as mentioned above and described below:

  • $index: It holds the index value where the new element is to be inserted.
  • $newval: It holds the element which to be inserted or added.

Return Value: It does not return any value.

Below programs illustrate the SplDoublyLinkedList::add() function in PHP:

Program 1:




<?php 
  
// Declare an empty SplDoublyLinkedList
$list = new \SplDoublyLinkedList;
  
// Use SplDoublyLinkedList::add() function to 
// add elements to the SplDoublyLinkedList
$list->add(0, 1); 
  
$list->add(1, "Geeks"); 
  
$list->add(2, "G"); 
  
$list->add(3, 10); 
  
print_r($list); 
?> 


Output:

SplDoublyLinkedList Object
(
    [flags:SplDoublyLinkedList:private] => 0
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 1
            [1] => Geeks
            [2] => G
            [3] => 10
        )

)

Program 2:




<?php 
  
// Declare an empty SplDoublyLinkedList
$list = new \SplDoublyLinkedList;
  
// Use SplDoublyLinkedList::add() function to 
// add elements to the SplDoublyLinkedList
$list->add(0, 30);
$list->add(1, 20);
$list->add(2, 30);
$list->add(3, "Geeks");
$list->add(4, 'G');
  
print_r($list); 
?> 


Output:

SplDoublyLinkedList Object
(
    [flags:SplDoublyLinkedList:private] => 0
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => 30
            [1] => 20
            [2] => 30
            [3] => Geeks
            [4] => G
        )

)

Reference: https://www.php.net/manual/en/spldoublylinkedlist.add.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