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

PHP - Ds Deque::join() Function



ThePHP Ds\Deque::join() function is used to join all values together of the current deque as a single String. The deque may contain various elements of any datatypes.

This function provides an optional parameter, which is used to separate the combined values with a specified string or separator. If no separator string is provided, the values will be joined together without separating.

Syntax

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

public Ds\Deque::join(string $glue = ?): string

Parameters

This function accepts an optional parameter called 'glue', which is described below −

  • glue (optional) − An optional string to separate each value.

Return value

This function returns all values of the deque joined together as a single string.

Example 1

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

<?php
   $deque = new \Ds\Deque([1, 2, 3, 4, 5]);
   echo "The deque elements are: \n";
   print_r($deque);
   echo "A joined values as a string: ";
   print_r($deque->join());
?>

Output

The above program displays the following output −

The deque elements are:
Ds\Deque Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
A joined values as a string: 12345

Example 2

Following is another example of thePHP Ds\Deque::join()function. We use this function to join all the values together of this deque (["Welcome", " ", "to", " ", "Tutorials", "Point"]) as a string −

<?php
   $deque = new \Ds\Deque(["Welcome", " ", "to", " ", "Tutorials", "Point"]);
   echo "The deque elements are: \n";
   print_r($deque);
   echo "A string is: ";
   print_r($deque->join());
?>

Output

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

The deque elements are: 
Ds\Deque Object
(
    [0] => Welcome
    [1] =>
    [2] => to
    [3] =>
    [4] => Tutorials
    [5] => Point
)
A string is: Welcome to TutorialsPoint

Example 3

If we pass an optional parameter "glue" value as acomma (,)to this function, each value will be separated by a comma in a resultant string as follows:

<?php
   $deque = new \Ds\Deque(['a','p','p','l','e']);
   echo "The deque elements are: \n";
   print_r($deque);
   $glue = ",";
   echo "The given separator is: ".$glue;
   echo "\nAfter the join operation, the result is: ";
   print_r($deque->join(','));
?>

Output

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

The deque elements are: 
Ds\Deque Object
(
    [0] => a
    [1] => p
    [2] => p
    [3] => l
    [4] => e
)
The given separator is: ,
After the join operation, the result is: a,p,p,l,e
Advertisements