In this tutorial, you shall learn how to join an array of strings with a separator string in PHP using join() function, with the help of example programs.

PHP – Join Array of Strings with Separator

To join an array of strings with specific separator string in PHP, use join() function. Call join() function and pass the separator string and the array of strings as arguments.

The syntax to call join() function to join the strings in array $arr by the separator $sep is

</>
Copy
join(' ', $input)

Examples

1. Join strings in array with specific separator

In this example, we take a string array in $arr and join them with separator string in $sep.

PHP Program

</>
Copy
<?php
$arr = ["apple", "banana", "cherry"];
$sep = "****";
$output = join($sep, $arr);
echo $output;
?>

Output

PHP - Join Array of Strings with Separator

2. Join strings in array with single space separator

In this example, we take a string array in $arr and join them with single space as separator.

PHP Program

</>
Copy
<?php
$arr = ["apple", "banana", "cherry"];
$sep = " ";
$output = join($sep, $arr);
echo $output;
?>

Output

PHP - Join Array of Strings with single space as Separator

Conclusion

In this PHP Tutorial, we learned how to join strings in array with specific separator, using join() function.