PHP join() Function
Definition
The join() function, an alias of the implode() function, returns a string from the elements of an array.
Syntax
PHP join() Function has the following syntax.
join(separator,array)
Parameter
Parameter | Is Required | Description |
---|---|---|
separator | Optional. | What to put between the array elements. Default is "" (an empty string) |
array | Required. | The array to join to a string |
Return
PHP join() Function returns a string from elements of an array.
Example 1
Join array elements with a string:
<?php
$arr = array('Hello','World!','from','java2s.com!');
echo join(" ",$arr);
?>
The code above generates the following result.
Example 2
Separate the array elements with different characters:
<?php// w w w . ja va 2 s.c o m
$arr = array('Hello','World!','from','java2s.com!');
echo join(" ",$arr)."\n";
echo join("+",$arr)."\n";
echo join("-",$arr)."\n";
echo join("X",$arr);
?>
The code above generates the following result.