What is the Ds\Set reversed() function in PHP?

The Ds\Set::reversed() function is used to reverse the order of elements in a set. The original set remains unaffected and a reversed copy of the set is created.

Syntax

The Ds\Set::reversed() function can be declared, as is shown in the code snippet below:

Ds\Set public Ds\Set::reversed ( void )

Note: The Ds\Set::reversed() function does not have any input parameters.

Return value

The Ds\Set::reversed() function returns a reversed copy of the set.

Example

Consider the code snippet below, which demonstrates the use of the Ds\Set::reversed() function:

<?php
$s1 = new \Ds\Set([1, 2, 3, 4, 5, 6, 7]);
echo("Set before reversed(): \n");
print_r($s1);
echo("Set after reversed(): \n");
print_r($s1->reversed());
?>

Explanation

  • Line 3: We declare the set s1.

  • Line 9: We reverse the order of the elements in s1, using Ds\Set::reversed().