PHP - Ds Vector reverse() Function



The PHP Ds\Vector::reverse() function is used to reverse the vector in-place. The term "in-place" means that the same vector is modified without creating a new vector or allocating new memory.

Once this function is called on a vector, the last element will be placed in the first position, the first element will be placed in the last position, and so on.

Syntax

Following is the syntax of the PHP Ds\Vector::reverse() function −

public void Ds\Vector::reverse ( void )

Parameters

This function does not accept any parameter.

Return value

This function does not return any value.

Example 1

The following is the basic example of the PHP Ds\Vector::reverse() function −

<?php  
   $vector = new \Ds\Vector([1, 2, 3, 4, 5]);
   echo("The original vector elements: \n");  
   print_r($vector);
   echo "The vector after reverse: \n";
   #using reverse() function
   $vector->reverse();
   print_r($vector); 
?>

Output

The above program produces the following output −

The original vector elements:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
The vector after reverse:
Ds\Vector Object
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
)

Example 2

Following is another example of the PHP Ds\Vector::reverse() function. We use this function to reverse this vector (["Tutorials", "Point", "India", "Tutorix"]) in-place −

<?php  
   $vector = new \Ds\Vector(["Tutorials", "Point", "India", "Tutorix"]);
   echo "The original vector: \n";   
   print_r($vector);
   echo "The vector after reversing: \n";
   #using reverse() function
   $vector->reverse();
   print_r($vector); 
?>

Output

After executing the above program, the following output will be displayed −

The original vector:
Ds\Vector Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
    [3] => Tutorix
)
The vector after reversing:
Ds\Vector Object
(
    [0] => Tutorix
    [1] => India
    [2] => Point
    [3] => Tutorials
)
php_function_reference.htm
Advertisements