PHP implode and explode functions are very easy to use. In this tutorial, we will learn how to do it. Let’s check it out.
There are some situations where we need to convert a string to array and vice versa to work with our logic, here implode and explode functions comes in handy. It’s easy to learn and implement.
The implode function implodes an array into a string, and the explode function explodes a string into an array. It’s very useful if you’ve stored your data in strings in files and want to convert those strings into array elements when your Web application runs.
Let’s take a look at an example below:
<?php $flower=”Lily”; $flower=”Rose”; $flower=”Tulip”; $text=implode(“, “,$flower); echo $text; ?>
This gives: Lily, Rose, Tulip
As we can see, we specify the string that separates the array items by “, “ (in this case).
So the syntax of implode is
Now, how about exploding this string back to the array? It’s very simple, to do that, we have to indicate which text we want to split, like “, “.
<?php $text=”Lily, Rose, Tulip”; $flower=explode(“, “,$text); Print_r($flower); ?>
This gives :
 => Lily
 => Rose
 => Tulip
The syntax of explode is
So as we can see, it’s nice and simple.