Friday, October 10, 2025
HomeLanguagesPHP | DsSequence set() Function

PHP | Ds\Sequence set() Function

The Ds\Sequence::set() function is an inbuilt function in PHP which is used to updates a value at a given index.

Syntax:

void abstract public Ds\Sequence::set ( int $index , mixed $value )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $index: It is used to hold the index number where value of sequence to be updated.
  • $value: It is used to hold the new value which to be update at given index.

Return value: This function does not return any value.

Below programs illustrate the Ds\Sequence::set() function in PHP:

Program 1:




<?php 
  
// Create new Sequence
$seq = new \Ds\Vector([1, 2, 3, 4, 5]); 
  
echo("Original Sequence\n"); 
  
// Display the Sequence elements 
print_r($seq); 
  
// Use set() function to update
// the sequence elements 
$seq->set(2, "Geeks");
  
echo("\nSequence after updating the elements\n"); 
  
// Display the Sequence elements 
print_r($seq); 
  
?> 


Output:

Original Sequence
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)

Sequence after updating the elements
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => Geeks
    [3] => 4
    [4] => 5
)

Program 2:




<?php 
  
// Create new Sequence
$seq = new \Ds\Vector(["Geeks", "for", "Geeks", 
        "Computer", "Science", "Portal"]); 
  
echo("Original Sequence\n"); 
  
// Display the Sequence elements 
print_r($seq); 
  
// Use set() function to set
// the sequence elements 
$seq->set(0, "Welcome");
$seq->set(1, "to");
$seq->set(2, "neveropen");
  
echo("\nSequence after updating the elements\n"); 
  
// Display the Sequence elements 
print_r($seq); 
  
?> 


Output:

Original Sequence
Ds\Vector Object
(
    [0] => Geeks
    [1] => for
    [2] => Geeks
    [3] => Computer
    [4] => Science
    [5] => Portal
)

Sequence after updating the elements
Ds\Vector Object
(
    [0] => Welcome
    [1] => to
    [2] => neveropen
    [3] => Computer
    [4] => Science
    [5] => Portal
)

Reference: https://www.php.net/manual/en/ds-sequence.set.php

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7100 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS