Back to Blog Page
Backend Development

PHP Implode and Explode

PHP Implode and Explode

Written by Kolade Chris | Dec 14, 2024 | #PHP | 3 minute Read

Dealing with strings and arrays is an everyday task for every programmer.

PHP provides two functions that make this task easier so you don’t break a sweat. Those two functions are the implode() and explode() functions.

In this article, I’ll show you how easy it is to work with arrays and strings using the implode() and explode() functions.

What are the implode() and explode() Functions?

The implode() and explode() functions perform opposite tasks.

implode() takes an array of strings and concatenates them into a single string, separating each array element with a specified delimiter [ or separator]. explode() on the other hand, takes a string and splits it into an array of substrings, using a specified delimiter to determine where to do the splitting.

How to Use the implode() Function

Remember that the implode() function takes an array and splits it into strings(). The basic syntax of the implode() function looks like this:

1
implode(separator, array)
  • separator is what you want to separate each array element with. For example, ,, -, or
  • array is the array you want to separate. That is, the array you want to use implode() on.

Here’s an example:

1
$myStringArray = ['Apple', 'Banana', 'Pineapple', 'Mango', 'Orange'];
2
$implodedStringArray = implode(', ', $myStringArray);
3
4
var_dump($myStringArray);
5
6
/* array(5) {
7
[0]=> string(5)
8
"Apple" [1]=> string(6)
9
"Banana" [2]=> string(9)
10
"Pineapple" [3]=> string(5)
11
"Mango" [4]=> string(6)
12
"Orange"
13
}
14
*/
15
16
echo $implodedStringArray; // Apple, Banana, Pineapple, Mango, Orange

You can go ahead and check the type of the imploded string, and you’ll see it’s indeed a string:

1
echo gettype($implodedStringArray); // string

That’s why you could echo it out in the first place!

How to Use the explode() Function

The explode function takes a string and splits the element into an array. Here’s what the basic syntax looks like:

1
explode(separator, string, limit)
  • separator: specifies the character you want to use to split the string into an array
  • string: specifies the string you want to use the explode() function on limit (optional): specifies the maximum number of elements to return in the array. If omitted, all occurrences of the separator in the string are used to split the string.

Here’s an example:

1
$myString = "Apple Banana Pineapple Mango Orange";
2
$explodedString = explode(' ', $myString);
3
4
echo $myString . "<br>"; // Apple Banana Pineapple Mango Orange
5
print_r($explodedString);
6
7
/* Array (
8
[0] => Apple
9
[1] => Banana
10
[2] => Pineapple
11
[3] => Mango
12
[4] => Orange
13
)
14
*/

You can format the resulting array by surrounding your print_r() with pre tags:

1
echo $myString . "<br>"; // Apple Banana Pineapple Mango Orange
2
echo '<pre>';
3
print_r($explodedString);
4
/*
5
Array
6
(
7
[0] => Apple
8
[1] => Banana
9
[2] => Pineapple
10
[3] => Mango
11
[4] => Orange
12
)
13
*/

PHP eplode resuslt

If you want the resulting array not to exceed a particular index, you can specify a limit:

1
echo $myString . "<br>"; // Apple Banana Pineapple Mango Orange
2
echo '<pre>';
3
print_r($explodedString);
4
/*
5
Array
6
Array
7
(
8
[0] => Apple
9
[1] => Banana
10
[2] => Pineapple Mango Orange
11
)
12
*/
13
echo '<pre>';

Conclusion

With implode() and explode(), handling strings in PHP becomes smoother and more efficient, giving your applications that extra bit of flexibility and flair.

Whether you’re putting together a string from an array or breaking down a sentence into its parts, these functions make it all a breeze.