Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP - Ds Sequence push() Function



The PHPDs\Sequence::push()function is used to add ( or say push) values to the end of a sequence. This function allows you to push multiple values at a time by specifying them in the function.

Syntax

Following is the syntax of the PHP Ds\Sequence::push() function −

abstract public Ds\Sequence::push(mixed ...$values): void

Parameters

This function accepts the following parameters −

  • values − Single or multiple values of any type needs to be pushed in.

Return value

This function does not return any value.

Example 1

The following program demonstrates the usage of the PHP Ds\Sequence::push() function −

<?php 
   $seq = new \Ds\Vector([]);
   echo "The sequence before push: \n";
   print_r($seq);
   $val = 10;
   echo "The given value: ".$val;
   echo "\nThe sequence after push: \n";
   #using push() function
   $seq->push($val);
   print_r($seq);
?>

Output

Following is the output of the above program −

The sequence before push:
Ds\Vector Object
(
)
The given value: 10
The sequence after push:
Ds\Vector Object
(
    [0] => 10
)

Example 2

Pushing multiple elements into a sequence at a time.

Following is another example of the PHP Ds\Sequence::push() function. We use this function to push multiple elements at a time to this sequence −

<?php 
   $seq = new \Ds\Vector(['a', 'b']);
   echo "The sequence before push: \n";
   print_r($seq);
   $val1 = 'c';
   $val2 = 'd';
   $val3 = 'e';
   echo "The given values are: ".$val1.", ".$val2.", ".$val3;
   echo "\nThe sequence after push: \n";
   #using push() function
   $seq->push($val1, $val2, $val3);
   print_r($seq);
?>

Output

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

The sequence before push:
Ds\Vector Object
(
    [0] => a
    [1] => b
)
The given values are: c, d, e
The sequence after push:
Ds\Vector Object
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
)

Example 3

In the example below, we use this function within the for-loop to add an element at the end of the current sequence −

<?php 
   $seq = new \Ds\Vector([1]);
   echo "The sequence before push: \n";
   print_r($seq);
   echo "\nThe sequence after push: \n";
   #using push() function
   for($i = 2; $i<10; $i++){
	   $seq->push($i);
   }
   print_r($seq);
?>

Output

The above program produces the following output −

The sequence before push:
Ds\Vector Object
(
    [0] => 1
)

The sequence after push:
Ds\Vector Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
)
php_function_reference.htm
Advertisements