PHP Tutorial PHP Advanced PHP Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

PHP - String join() Function



The PHP String join() function returns a string which is constructed from elements of an array separated by specified separator.

Note:
PHP 7.4.0 - Passing the separator after the array has been deprecated.
PHP 8.0.0 - Passing the separator after the array is no longer supported.

Syntax

join(separator, array)

Parameters

separator Optional. Specify the separator used for joining the elements of the array. Default: an empty string ("")
array Required. Specify the array whose elements need to be joined.

Return Value

Returns a string which is constructed from elements of an array separated by specified separator.

Example:

In the example below, join() function returns a string from elements of a given array.

<?php
$Arr = array("Hello", "World");

//constructing string using default separator
$str = join($Arr);

echo $str;
?>

The output of the above code will be:

HelloWorld

Example:

A separator can be used with join() function. Consider the following example.

<?php
$Arr = array(10, 20, 30);

//constructing string using default separator
$str1 = join($Arr);

//constructing string using whitespace as separator
$str2 = join(" ", $Arr);

echo "str1 is: $str1 \n";
echo "str2 is: $str2 \n";
?>

The output of the above code will be:

str1 is: 102030 
str2 is: 10 20 30 

❮ PHP String functions

5