Thursday, October 23, 2025
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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS