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

PHP - Ds\Queue::push() Function



The PHP Ds\Queue::push() function is used to insert values into the current queue. A "queue" is a linear data structure that follows the FIFO (First In, First Out) order, which means that the first value-added will be the first and will be removed first as well.

This function also allows you to add multiple values at once.

Syntax

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

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

Parameters

Following is a parameter of this function −

  • values − Single or multiple values need to be pushed into a queue.

Return value

This function does not return any value.

Example 1

The following program demonstrates the usages of the PHP Ds\Queue::push() function −

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

Output

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

The original queue is:
Ds\Queue Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
)
The given value is: 50
The queue after push:
Ds\Queue Object
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
)

Example 2: Pushing multiple values into an empty ([]) queue

Following is another example of the PHP Ds\Queue::push() function. We use this function to push the elements "Tutorials", "Point", and "India" into this queue ([])

<?php  
   $queue = new \Ds\Queue([]);
   echo "The queue before push: \n";
   print_r($queue);
   $val1 = "Tutorials";
   $val2 = "Point";
   $val3 = "India";
   echo "The given values are: $val1, $val2, $val3";
   #using push() function
   $queue->push($val1, $val2, $val3);
   echo "\nThe queue after push: \n";
   print_r($queue);
?>

Output

The above program produces the following output −

The queue before push:
Ds\Queue Object
(
)
The given values are: Tutorials, Point, India
The queue after push:
Ds\Queue Object
(
    [0] => Tutorials
    [1] => Point
    [2] => India
)

Adding values dynamically using for-loop

The for-loop in PHP is a control statement used to iterate through a given list, array, or collection, and allows you to execute a block of code repeatedly for each element.

Example

In this example, we use the push() function within the for-loop to insert elements continuously till the condition is satisfied −

<?php  
   $queue = new \Ds\Queue([1, 2]);
   echo "The original queue is: \n";
   print_r($queue);
   echo "The queue after push:\n";
   for($i = 3; $i<=10; $i++){
	   $queue->push($i);
   }
   print_r($queue);
?>

Output

Following is the output of the above program −

The original queue is:
Ds\Queue Object
(
    [0] => 1
    [1] => 2
)
The queue after push:
Ds\Queue Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
)
php_function_reference.htm
Advertisements