Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php

What is the Ds\Vector set() function in PHP?

Hammad Nasir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The Vector class in PHP is an array that is automatically resized. The elements of a Vector instance are accessible via indexes.

This shot discusses the set method of the Vector class.

Description

The set method changes the value at the specified index inside the Vector.

Syntax

<?php
 public Ds\Vector::set(int $index, mixed $value): void
?>

Parameters

  • $index is an integer that specifies the index of the Vector where the value is to be set.
  • $value is the value that is to be set at the specified $index of the Vector.

Return Value

None.

Exceptions

  • OutOfRangeException is thrown if the index is not valid (e.g. is not in range).

Example

<?php
$vec = new \Ds\Vector([1, 2, 3]);
$vec->set(2, 100);
print_r($vec);
?>

Output:

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

In the example above, a vector $vec is initialized with the values 1, 2, and 3. The set method is invoked on $vec with $index value 2 and $value as 100. This method call results in the last element of $vec to be set to the value 100.

RELATED TAGS

php

CONTRIBUTOR

Hammad Nasir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring