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

PHP - Ds Deque::push() Function



The PHP Ds\Deque::push() function is used to append the specified value at the end of the current deque. This function allows you to push single or multiple values simultaneously, and all specified values are added one by one at the end of the deque.

You can also push values into an empty deque ([]).

Syntax

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

public void Ds\Deque::push([ mixed $...values ] )

Parameters

This function accepts a single parameter named 'values', which is described below −

  • values − A single or multiple values need to be append.

Return value

This function does not return any value.

Example 1

The following is the basic example of the PHP Ds\Deque::push() function −

<?php
   $deque = new \Ds\Deque([10, 20, 30, 40, 50]);
   echo "The deque elements are: \n";
   print_r($deque);
   $val = 100;
   echo "The given value is: ".$val;
   #using push() function
   $deque->push($val);
   echo "\nThe deque after push: \n";
   print_r($deque);
?>

Output

The above program produces the following output −

The deque elements are:
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)
The given value is: 100
The deque after push:
Ds\Deque Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 100
)

Example 2

The following is another example of the PHP Ds\Deque::push() function. We use this function to add values 5 and 6 in this deque ([1, 2, 3, 4]) −

<?php
   $deque = new \Ds\Deque([1, 2, 3, 4]);
   echo "The deque elements are: \n";
   print_r($deque);
   $val1 = 5;
   $val2 = 6;
   echo "The given values are: ".$val1.", ".$val2;
   #using the push() function
   $deque->push($val1);
   $deque->push($val2);
   echo "\nThe deque after push: \n";
   print_r($deque);
?>

Output

On executing the above program, it will display the following output −

The deque elements are:
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)
The given values are: 5, 6
The deque after push:
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)

Example 3

Adding values to an empty ([]) deque.

In the example below, we use the push() function to add elements 22, 32, 42, and 52 to the empty deque ([]) as follows:

<?php
   $deque = new \Ds\Deque([]);
   echo "The original deque: \n";
   print_r($deque);
   $deque->push(...[22, 32, 42, 52]);
   echo "The deque after push: \n";
   print_r($deque);
?>

Output

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

The original deque:
Ds\Deque Object
(
)
The deque after push:
Ds\Deque Object
(
    [0] => 22
    [1] => 32
    [2] => 42
    [3] => 52
)
Advertisements