PHP Tutorial - 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

ParameterIs RequiredDescription
separatorOptional. What to put between the array elements. Default is "" (an empty string)
arrayRequired. 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
$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.