What is PHP join() Function?
If you want to create a string by joining all the elements of an array, use PHP join() function.
Syntax:
join(separator, array)
Parameters:
The function has 1 required parameter and 1 optional parameter-
separator (Optional): it specifies the separator used when joining two array elements. The default value is empty string (“”). Check examples 1 & 2.
array (Required): It specifies an array.
Return Values:
The function returns a string that contains all the array elements joined by the separator.
Examples:
Example 1: Joining array elements with empty separator and no separator-
<?php
$array = array("Learn", "PHP","From","SOW");
echo implode("", $array);
echo"<br />";
echo implode($array);
?>
Output:
LearnPHPFromSOW
LearnPHPFromSOW
Explanation:
When you try to join all the elements with an empty separator, there remains no space between elements in the resultant string. The same result you can get when you don’t use the separator parameter. In this case, the function uses the empty string itself as the separator.
Example 2: Joining array elements with one character or multi-character separator-
<?php
$array = array("Learn","MySQL","From","SOW");
echo implode(" -> ", $array);
echo "<br />";
echo implode(" | ", $array);
echo "<br />";
echo implode(" ", $array);
echo "<br />";
echo implode(", ", $array);
echo "<br />";
echo implode("<br />", $array);
?>
Output:
Learn -> MySQL -> From -> SOW
Learn | MySQL | From | SOW
Learn MySQL From SOW
Learn, MySQL, From, SOW
Learn
MySQL
From
SOW
Explanation:
You can use different characters as separators. Check line 3 to line 11.
Example 3: join() function increases readability of an array to convert it to string-
<pre>
<?php
$array = array("HTML","CSS","JavaScript","Bootstrap", "React", "Wordpress");
echo "Skills needed to become a web developer- ";
print_r($array);
echo "<br />";
echo "Skills needed to become a web developer- ";
echo implode(", ", $array);
?>
</pre>
Output:
Skills needed to become a web developer- Array
(
[0] => HTML
[1] => CSS
[2] => JavaScript
[3] => Bootstrap
[4] => React
[5] => WordPress
)
Skills needed to become a web developer- HTML, CSS, JavaScript, Bootstrap, React, WordPress
Explanation:
join() increases readability. The string that the function displays at line 8 has much more readability than the string that generates by the line 5.
Example 4: join() can operator on binary values-
<?php
$array = array("Learn","JavaScript","From","SOW");
echo implode(" \x2D ", $array);
?>
Output:
Learn - JavaScript - From - SOW
Explanation:
“\x2D” is a hexadecimal value of character dash (-). The function use this as separator to join the array elements.
Example 5: join() can also operators on Boolean values-
<?php
$array = array(TRUE, "FALSE", FALSE, TRUE);
echo implode(", ", $array);
?>
Output:
1, FALSE, , 1
Explanation:
The first, third, and fourth elements are Boolean values. join() can join these values and create a string.
Example 6: join() can’t join elements of an multi-dimensional array-
<?php
$array=array(
array('PHP','Python'),
array('MySQL','MongoDB')
);
echo implode(", ", $array);
?>
Output:
Warning: Array to string conversion in …\join.php on line 6
Warning: Array to string conversion in …\join.php on line 6
Explanation:
join() function can’t join elements of a multi-dimensional array. It displays warning.
Practical Usages of join() Function:
This function has lots of practical usages. Few usages are-
- You can’t insert an array into a database table directly. Instead, you can convert the array into a string by this function and insert this into database table as a string. Later, when you need this data, you can convert this string back to an array using other built-in functions like explode(). Look at the sample code below-
<?php $arr = array("PHP","MySQL","HTML5","CSS3"); $string="INSERT INTO table('column') VALUES($arr)"; ?> - As join() function can create comma separated string. It is useful to create rows for a CSV file.
<?php $arr=array("PHP","20.87%","1993"); $row=implode(",",$arr); ?> - Sometimes it becomes easier to verify data from a string than an array. In this case, you can use join() function to convert an array to a string.
- For presentation purpose, a string is easier to read than an array. implode() function can be used in this instance. Check example 3.
Notes on join() Function:
- It is an alias of implode() function.
- Though join() function do the same tasks as implode() function, developers usually use the implode() function.
- The function doesn’t modify the input array.
- It is a binary safe function. So, this can operate on binary data if one is found in its parameter. Check example 4.
- The function not only operates on array that only contains string or numbers. You can convert an array to string that contains only Boolean values. In the resulting string, the function will convert Boolean TRUE to 1. Check example 5.
- The explode() function only works on flat array or one-dimensional array, it doesn’t work on multi-dimensional array or nested array. Check example 6.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP join() Function
join() function is one of the built-in string functions. Use this function to convert an array to a string.