php

Use of join() Function in 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.

//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 "

The values of join() without using delimiter:

". $string."

" ;
?>

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.

//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 "

The values of the join() with '$' delimiter:

". $string."

" ;
?>

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.

//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 "

The values of the join() with '+' delimiter:

". $string." = ". $sum . "

" ;
?>

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.

//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 "

The values of the first join() with '-' delimiter:

". $string1 . "

";
//Print the return value of $string2
echo "

The values of the second join() with '-' delimiter:

". $string2 . "

";
?>

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(
) delimiter

The following example shows the use of the join() function with the '
' 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.

//Define an array of even numbers
$even_numbers = array(2, 4, 6, 8, 10);
//Combine the array values with

$string = join('
',$even_numbers);
//Print the return values of the join() function
echo "

The list of even numbers [1 - 10]:

". $string."

";
?>

Output:

The following output will appear after running the above script from the webserver. It shows each array value in each line for the '
' 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.

Open Source Ports of Commercial Game Engines
Free, open source and cross-platform game engine recreations can be used to play old as well as some of the fairly recent game titles. This article wi...
Linux için En İyi Komut Satırı Oyunları
Komut satırı, Linux kullanırken yalnızca en büyük müttefikiniz değil, aynı zamanda eğlence kaynağı da olabilir, çünkü onu özel bir grafik kartı gerekt...
Linux için En İyi Gamepad Eşleme Uygulamaları
Tipik bir klavye ve fare giriş sistemi yerine bir gamepad ile Linux'ta oyun oynamayı seviyorsanız, sizin için bazı faydalı uygulamalar var. Çoğu PC oy...