Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PHP | Imploding and Exploding

  • Last Updated : 08 Mar, 2018

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.

implode() method

Syntax:

string implode (glue ,pieces)

or,

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:




<?php
// PHP code to illustrate the working of implode()
  
$array1 = array('www', 'geeksforgeeks', 'org');
echo(implode('.',$array1)."<br>"); 
  
$array2 = array('H', 'E', 'L', 'L', 'O');
echo(implode($array2));
?>

Output:

www.geeksforgeeks.org
HELLO

You may refer to the article on PHP | implode() function to learn about implode() in details.

explode() method

Syntax:

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:




<?php
// PHP code to illustrate the working of explode()
  
$str1 = '1,2,3,4,5';
$arr = explode(',',$str1);
foreach($arr as $i)
echo($i.'<br>');
?>

Output:

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.



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!