PHP implode() function joins all the elements of an array with the provided joiner string/character and returns a string. In this function, we pass 2 parameters i.e. joiner, array.

Unlike PHP explode function, it doesn’t have any 3rd parameter.

implode( joiner, array )
joineroptionalA character or string that is placed between the elements of array while joining.
The default value is “” (an empty string)
arrayrequiredThe array whose elements will be joined

Example

Joining elements of an array of person names using PHP implode function.

$names_arr = array( 'John', 'David', 'Raj', 'Emily', 'Tina' );

// join using comma with a space ", "
$names = implode(', ', $names_arr);
/* Output:
John, David, Raj, Emily, Tina
*/

// join using comma with a space ", "
$names = implode('-', $names_arr);
/* Output:
John-David-Raj-Emily-Tina
*/

// join without 'joiner' parameter
$names = implode($names_arr);
/* Output:
JohnDavidRajEmilyTina
*/

Example Explained:
Well, the example itself explains everything. This is so simple. But, still, let me explain a little bit of what happened here.

In first 2 cases, we have provided joiner as “, ” (comma with space) and “-” (dash character). All the elements are joined using these strings in between them.

In 3rd case of PHP implode function, we haven’t provided the first parameter i.e. joiner. So, all the elements are joined without any joiner between them.

Note: The parameters’ placement inside the implode function can be switched. I mean, you can pass 1st parameter as array() and 2nd parameter as joiner as well. But it is recommended that you follow the standard.

I hope you have understood the implode function in PHP very well, please click on the bell icon on right bottom corner of the page to subscribe so that you will get notifications of our future posts.