PHP Function Reference

PHP implode() Function



The PHP implode() function returns a string which is constructed from elements of an array separated by an empty string or a 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.

Note: This function is binary-safe.

Syntax

implode(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 containing a string representation of all the array elements in the same order, with the separator string between each element.

Example:

In the example below, implode() function returns a string constructed from elements of a given array and specified separator.

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

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

echo $str;
?>

The output of the above code will be:

HelloWorld

Example:

A separator can be specified using the optional parameter. Consider the following example:

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

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

//constructing string using whitespace as separator
$str2 = implode(" ", $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 Reference