Tuesday, December 23, 2025
HomeLanguagesPHP | DsVector reverse() Function

PHP | Ds\Vector reverse() Function

The Ds\Vector::reverse() function is an inbuilt function in PHP which is used to reverse the vector elements in-place.

Syntax:

void public Ds\Vector::reverse( void )

Parameters: This function does not accepts any parameters.

Return Value: This function does not return any value.

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

Program 1:




<?php 
  
// Create new Vector 
$arr = new \Ds\Vector([1, 2, 3, 4, 5]); 
  
// Display the elements 
print_r($arr); 
  
echo("Vector after reversing\n"); 
  
// Use reverse() function to reverse 
// the vector elements and display it 
$arr->reverse();
  
print_r($arr);
  
?>


Output:

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

Program 2:




<?php 
  
// Create new Vector 
$arr = new \Ds\Vector(["Geeks", "GFG",
        "Computer", "Science", "Portal"]); 
  
// Display the elements 
print_r($arr); 
  
echo("Vector after reversing\n"); 
  
// Use reverse() function to reverse 
// the vector elements and display it 
$arr->reverse();
  
print_r($arr);
  
?>


Output:

Ds\Vector Object
(
    [0] => Geeks
    [1] => GFG
    [2] => Computer
    [3] => Science
    [4] => Portal
)
Vector after reversing
Ds\Vector Object
(
    [0] => Portal
    [1] => Science
    [2] => Computer
    [3] => GFG
    [4] => Geeks
)

Reference: https://www.php.net/manual/en/ds-vector.reverse.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6825 POSTS0 COMMENTS
Nicole Veronica
11959 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6912 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS