Introduction to PHP implode
PHP implode function is one of the functions built-in used for joining elements of an array. This function works similarly to join() and is an alias of the same.
We use the implode function in an array of elements to join all of them to make a string. Array elements are usually joined by the help of a string and use a delimiter as specified by us. Hence implode function gives us the string resulting from the formation of elements of the array similar to the join() function.
Parameter: Two parameters are required as an input to this function where one is optional and the other is a mandatory one.
- separator: This is a non-mandatory input component of type string. The array values here will be joined to make a string and will be distinguished with the help of a separator parameter given here. When it is not given default value is taken which is an empty string (“”).
- array: This is a mandatory field that specifies the array that has to be joined to create the string.
Note: For backward compatibility, it is always advised to use both the parameters even though the separator is not mandatory.
Return type: This implode() function returns string as its output. From the array elements, it will return the string which is newly joined.
Examples of PHP implode
Below are some of the examples based on implode function which covers a few possible scenarios where they are or can be implemented:
// Below is PHP Code representing implementation of implode function
$Input = array('first','string','combination');
// Using function without separator parameter
// Using function with separator
In this basic example, we are showing how to use implode function both with and without using the non-mandatory separator parameter. So in the first line of output, it prints the array by joining all 3 strings mentioned in the input as it is. In the second line of output, we have the array output as a combination of the input strings but along with it and the separator parameter is also printed in between two words.
$arr = array('string1', 'string2', 'string3');
//Using implode function to make the strings as comma seperated
$sep= implode(",", $arr);
// When an empty array is used returns an empty string
var_dump(implode('check', array())); // string(0) ""
In this example, we are first declaring 3 strings as part of an array “arr”. Next, we are using implode function and mentioning the comma separator to use it for separating these 3 strings. We are also displaying what happens when an empty array is used. It returns an empty string in this case as shown.
//Declaring 3 different array lists
$arr1 = array("1","2","3");
$arr2 = array("one");
$arr3 = array();
echo "array1 is: '".implode("'/'",$arr1);
echo "array2 is: '".implode("'-'",$arr2);
echo "array3 is: '".implode("','",$arr3);
In this example, we will be showing three different cases of arrays. The first line of output displays when the array is having 2 or more strings in its array elements and we are joining these using implode function and / as the delimiter. The second line of output displays when the array is having a single element and we are using “-” delimiter for the same. The third line of output shows what happens when there is an empty array. The output will be printed as is without any errors.
$arr1 = array('One', 'Two', 'Three');
echo "<ol><li>" . implode("</li><li>", $arr1) . "</li></ol>";
In this example, we are displaying how to implode function can also be used in the case of HTML tags by using tags as the separators. Here we are making using the array to display its elements in the form of ordered lists.
$arr1 = array( 'str1','str2','str3' );
$arr2 = array( '1st' => 'one', 'two', '2nd' => 'three' );
echo implode( '-', $arr1 ),'.', implode( '-', $arr2 );
In this example, we can see that the implode function acts upon only the values of array elements and completely disregards its keys. Here ‘str1′,’str2′,’str3’ are the values directly declared in arr1 whereas in arr2 the keys are “1st”, “2nd” and their respective value pairs are “one”,”two” and “three”.
public function __construct($name)
$this->name = $name;
public function __toString()
$arr = [
echo implode('; ', $arr);
In the above example, we can see that even objects can be used alongside the implode function but the only condition for this is that the objects should apply the toString() function as shown.
var_dump(implode('',array(true, false, false, true, true)));
In this example, we are displaying what happens when a boolean set of values are used as array elements. It results in a different kind of output where we get the output in the form of 1’s wherever true is present and in place of false, it outputs null i.e empty value.
PHP implode() function as shown in the above examples can be used in various cases where there is a need to join different elements of an input array. It is a simple function having only 2 parameters where we specify the delimiter to be used to separate the array elements.
This is a guide to PHP implode. Here we discuss the introduction to implode function along with the syntax, parameters and respective examples. You can also go through our other suggested articles to learn more –