Friday, October 10, 2025
HomeLanguagesPHP | DsVector push() Function

PHP | Ds\Vector push() Function

The Ds\Vector::push() function is an inbuilt function in PHP that is used to add elements to the end of the vector. 

Syntax:

void public Ds\Vector::push( $values )

Parameters: This function accepts a single parameter $values which contains one or more than one element to be added to the vector. 

Return Value: This function does not return any values. 

The below programs illustrate the Ds\Vector::push() function in PHP

Program 1: 

PHP




<?php
 
// Create new vector
$arr1 = new \Ds\Vector([10, 20, 30, 40, 50]);
 
echo("Original vector elements\n");
print_r($arr1);
 
// Use push() function to add
// element to the vector
$arr1->push(60);
 
echo("\nVector after appending the elements\n");
print_r($arr1);
 
?>


Output:

Original vector elements
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

Vector after appending the elements
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
)

Program 2: 

PHP




<?php
 
// Create new vector
$arr1 = new \Ds\Vector([10, 20, 30, 40, 50]);
 
echo("Original vector elements\n");
print_r($arr1);
 
// Use push() function to add
// element to the vector
$arr1->push(...[60, 70]);
 
echo("\nVector after appending the elements\n");
print_r($arr1);
 
?>


Output:

Original vector elements
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

Vector after appending the elements
Ds\Vector Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
    [6] => 70
)

Reference: http://php.net/manual/en/ds-vector.push.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
7099 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS