Imploding and Exploding are couple of important functions of PHP that can be applied on strings or arrays. PHP provides us with two important builtin functions implode() and explode() to perform these operations. As the name suggests Implode/implode() method joins array elements with a string segment that works as a glue and similarly Explode/explode() method does the exact opposite i.e. given a string and a delimiter it creates an array of strings separating with the help of the delimiter.
string implode (glue ,pieces)
string implode (pieces)
Parameters:The function accepts two parameters as described below.
- glue (optional): This parameter expects a string segment that is used as the glue to join the pieces of the array. This is an optional parameter with default value being an empty string.
- pieces: This parameter expects an array whose elements are to be joined together to construct the final imploded string.
Return Type: This function traverses the input array and concatenates each element with one glue segment separating them to construct and return the final imploded string.
Below program illustrates the working of implode() in PHP:
You may refer to the article on PHP | implode() function to learn about implode() in details.
array explode (delimiter, string, limit)
Parameters:The function accepts three parameters as described below.
- delimiter: This parameter expects a string segment that can be used as the separator. If the delimiter itself is not present in the string then the resultant will be an array containing the string as its sole element.
- string: This parameter expects a string which is to be exploded.
- limit: This parameter expects an integer (both positive and negative). This is an optional parameter with default value of PHP_INT_MAX. The limit refers to the maximum number of divisions that are to be made on the input string.
Return Type: This function returns an array of strings containing the separated segments.
Below program illustrates the working of explode() in PHP:
1 2 3 4 5
You may refer to the article on PHP | explode() function to learn about explode() in details.
Important Points to Note:
- The implode() function implodes the elements of an array and returns the resultant string.
- Although not advised, the implode() function can take the parameters irrespective of their order
- The PHP | Join() function is an alias of implode() function.