Thursday, November 20, 2025
HomeLanguagesPHP | DsVector set() Function

PHP | Ds\Vector set() Function

The Ds\Vector::set() function is an inbuilt function in PHP which is used to set the value in the vector at the given index.

Syntax:

void public Ds\Vector::set( $index, $value )

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

  • $index: This parameter holds the index value at which the vector value to be updated.
  • $value: This parameter holds the value by which previous element is to be replaced.

Return Value: This function does not return any value.

Exception: This function throws OutOfRangeException if index is invalid.

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

Program 1:




<?php
  
// Create new Vector
$vect = new \Ds\Vector([1, 2, 3, 4, 5]);
  
// Display the Vector elements
print_r($vect);
  
// Use set() function to set the 
// element in the vector
$vect->set(1, 10);
  
echo("\nVector after updating the element\n");
  
// Display the vector elements
print_r($vect);
  
?>


Output:

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

Vector after updating the element
Ds\Vector Object
(
    [0] => 1
    [1] => 10
    [2] => 3
    [3] => 4
    [4] => 5
)

Program 2:




<?php
  
// Create new Vector
$vect = new \Ds\Vector(["neveropen", "of", "neveropen"]);
  
// Display the Vector elements
print_r($vect);
  
// Use set() function to set the 
// element in the vector
$vect->set(1, "for");
  
echo("\nVector after updating the element\n");
  
// Display the vector elements
print_r($vect);
  
?>


Output:

Ds\Vector Object
(
    [0] => neveropen
    [1] => of
    [2] => neveropen
)

Vector after updating the element
Ds\Vector Object
(
    [0] => neveropen
    [1] => for
    [2] => neveropen
)

Reference: http://php.net/manual/en/ds-vector.set.php

RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6776 POSTS0 COMMENTS
Nicole Veronica
11924 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6905 POSTS0 COMMENTS
Ted Musemwa
7160 POSTS0 COMMENTS
Thapelo Manthata
6861 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS