php

Use of join() Function in PHP

join() function works similarly as the other built-in function of PHP named implode(). It is used to create a string value by combining the values of the array. This function uses a particular delimiter to combine the array values. The empty string(”) is used as the default delimiter of this function if no delimiter is used. How the join() function can be used in PHP is shown in this tutorial.

Syntax:

join() function can be used with one argument or two arguments. The syntaxes of this function are given below:

string join(array $array)

When the join() function is called with an array variable, only then, it returns a string by combining the array elements with an empty string. This means no separator will be used between the elements.

string join(string delimiter, array $array)

When the join() function is called with a delimiter and an array, it returns a string by combining the array elements with the provided delimiter.

Example 1: Use of the join() function without delimiter

The following example shows the join() function without delimiter to combine the array values with an empty string. Create a PHP file with the following script.

An array named $languages is declared with the five string values. Here, the join() function will return a string by combining the array values with empty string.

<?php

        //Define an array of string values

        $languages = array('English', 'Bangla', 'Arabic', 'French', 'Hindi');

        //Combine the array values with empty string

        $string = join($languages);

        //Print the return values of the join() function

        echo "<h2> The values of join() without using delimiter: </h2><p style='color:Blue'>". $string."</p>" ;

?>

Output:

The following output will appear after running the above script from the webserver. It shows the array values as a string.

Example 2: Use of the join() function with dollar($) delimiter

The following example shows the use of the join() function with the dollar($) delimiter. Create a PHP file with the following script.

An associative array of two elements is declared in the script. The join() function will return a string by combining the two values of the array with the delimiter.

<?php

        //Define a two-dimensional array

        $array = array('str' => 'The price of the water color is ', 'price' => 10);

        //Combine the array values with '$' delimiter

        $string = join('$', $array);

        //Print the return values of the join() function

        echo "<h2> The values of the join() with '$' delimiter: </h2><p style='color:Blue'>". $string."</p>" ;

?>

Output:

The following output will appear after running the above script from the webserver. It shows the array values as a string by adding a dollar($) between the values.

Example 3: Use of the join() function with plus(+) delimiter

The following example shows the use of the join() function with the plus(+) delimiter. Create a PHP file with the following script.

A numeric array of four elements is declared in the script. The array_sum() function is used in the script to calculate the sum of the array values. The join() function will return a string by combining the array values with the plus(+) delimiter. Then, the script will print the return values of the join() and array_sum() functions.

<?php

        //Define an array of numbers

        $numbers = array(10,78,45,12);

        //Calculate the sum of array values

        $sum = array_sum($numbers);

        //Combine the array values with '+' delimiter

        $string = join('+', $numbers);

        //Print the return values of the join() function

        echo "<h2> The values of the join() with '+' delimiter: </h2><p style='color:Blue'>". $string." = ". $sum . "</p>" ;

?>

Output:

The following output will appear after running the above script from the webserver. It shows the array values as a string by adding plus(+) between the values, and the sum of the array values 145.

Example 4: Use of the join() function with hyphen(-) delimiter

The following example shows the use of the join() function with the hyphen(-) delimiter. Create a PHP file with the following script.

Two associative arrays of two elements are declared in the script. The join() function will return a string by combining the two values of each array with the hyphen(-). Both returned values will be printed later.

<?php

        //Define two arrays

        $array1 = array('site' => 'LinuxHint ', 'description' => ' It is a popular blog site.');

        $array2 = array('site' => 'Google ', 'description' => ' It is a popular search engine.');


        //Combine the array values with '-'

        $string1 = join('-', $array1);

        $string2 = join('-', $array2);

        //Print the return value of $string1

        echo "<h2> The values of the first join() with '-' delimiter: </h2><p style='color:Blue'>". $string1 . "</p>";

        //Print the return value of $string2

        echo "<h2> The values of the second join() with '-' delimiter: </h2><p style='color:Blue'>". $string2 . "</p>";

?>

Output:

The following output will appear after running the above script from the webserver. It shows both array values as a string by adding a hyphen(-) between the values.

Example 5: Use of join() function with break(<br/>) delimiter

The following example shows the use of the join() function with the ‘<br/>’ as a delimiter to print each value of the array in each line. Create a PHP file with the following script.

The numeric array of five even numbers is declared in the script. The join() function will return a string by adding a break between the values of the array that will be printed later.

<?php

       //Define an array of even numbers

        $even_numbers = array(2, 4, 6, 8, 10);

        //Combine the array values with <br />

        $string = join('<br />',$even_numbers);

        //Print the return values of the join() function

        echo "<h2> The list of even numbers [1 - 10]: </h2><p style='font-size:20px;color:Blue'>". $string."</p>";

?>

Output:

The following output will appear after running the above script from the webserver. It shows each array value in each line for the ‘<br/>’ delimiter.

Conclusion

The use of the join() function is explained in this tutorial by using different types of examples. This tutorial will help the readers understand the methods of using the join() function and apply this function in their script properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.