What is the difference between implode and explode in PHP?

The two functions implode() and explode() are in-built PHP functions. Before knowing the difference between these two functions, we will know how these functions work.

implode function

We will know the definition, syntax, parameters, return values, and an example code of the implode() function one by one.

Definition

The implode functions join array elements to a string.

Syntax

The syntax of the implode function is as follows:

implode(separator, array);

Parameters

This function has two parameters – separator and array.

separator – this parameter specifies what will be placed between the array elements. This parameter is optional and the default value is an empty string.

array – this parameter is the array whose elements are to be joined.

Return Values

This function returns a string. The returned string contains a string representation of all the array elements in the same order, with the separator string between each element.

Example

Example with separator

The example below has an array whose elements are [“blue”, “green”, “red”]. We will use the implode function to convert the array to a string whose output will be: blue, green, red. In this output, we can see that the elements of the array have been converted to strings in the same order i.e. first blue then green then red as the elements were in the array

<?php
$colors = ["blue", "green", "red"];
$stringColor = implode(", ", $colors);
echo $stringColor;
?>
Example without separator

We will use the same array as above for this example, except we will not pass the separator during the implode function call.

<?php
$colors = ["blue", "green", "red"];
$stringColor = implode($colors);
echo $stringColor;
?>

The output of the above code is: bluegreenred

I discussed the implode function in a previous article at: How to convert an array to a string in PHP.

We can learn more about implode function at: https://www.php.net/manual/en/function.implode.php

explode function

Now we will know the definition, syntax, parameters, return values, and an example code of the explode() function one by one.

Definition

The explode function splits a string by a string and generates an array.

Syntax

The syntax of the explode function is as follows:

explode(separator,string,limit)

Parameters

This function has three parameters – separator, string, and limit.

separator – This parameter is used to specify where to split the string. This is a required field.

string – This parameter is the string to split. This is a required field.

limit – This parameter specifies the number of array elements that are to be returned. This is an optional field.

Return Values

This function returns an array of strings.

Example

Example without limit

The following example is a string that is ‘blue, green, red’. From here we will create an array that is [“blue”, “green”, “red”]. In this example, we are not using the option permit limit.

<?php
$str = 'blue, green, red';
$arr = explode(', ',$str);
var_dump($arr);
?>

Using the var_dump function we get the array information:

array(3) { [0]=> string(4) "blue" [1]=> string(5) "green" [2]=> string(3) "red" }

Here we can see that the output is an array whose elements are strings

Example with limit

We will try to understand how limit works in the explode function from the output of the following code.

<?php
$str = 'blue, green, red';
$arr = explode(', ',$str, 0);
var_dump($arr);

echo '<br>';

$str = 'blue, green, red';
$arr = explode(', ',$str, 1);
var_dump($arr);

echo '<br>';

$str = 'blue, green, red';
$arr = explode(', ',$str, 2);
var_dump($arr);

echo '<br>';

$str = 'blue, green, red';
$arr = explode(', ',$str, -1);
var_dump($arr);

echo '<br>';

$str = 'blue, green, red';
$arr = explode(', ',$str, -2);
var_dump($arr);
?>  

The output of the above code is:

array(1) { [0]=> string(16) "blue, green, red" }
array(1) { [0]=> string(16) "blue, green, red" }
array(2) { [0]=> string(4) "blue" [1]=> string(10) "green, red" }
array(2) { [0]=> string(4) "blue" [1]=> string(5) "green" }
array(1) { [0]=> string(4) "blue" }

From this output we can see that:

  1. If limit is 0, the returned array contains 1 element
  2. If limit is positive then the return array contains that number of elements. That is if the limit is 1 then the returned array has 1 element and if the limit is 2 then the returned array has 2 limits.
  3. If limit is negative, then the number of elements in the return array is the total number of elements in the array minus that number, and that number of elements will be dropped from the end. In this case, if limit is 1 the returned array will have two elements, and if limit is 2 then the returned array will have one element.

We can learn more about explode function at: https://www.php.net/manual/en/function.explode.php

Difference between implode and explode

We have covered both the implode and explode functions in detail. Now we can easily sum up the difference between these two functions.

#implode functionexplode function
1implode function is applied on an arrayexplode function is applied on a string
2implode function returns a stringexplode function returns an array.
3implode function has two parameters –
separator(optional) and array
explode function has three parameters –
separator, string, and limit(optional)
4separator parameter is optional in implode
function
separator parameter is required in
explode function. if separator is empty string
then explode function throws an error.