PHP Bang and bang


Imploding and Exploding - these are a couple of important PHP functions that can be applied to strings or arrays. PHP provides us with two important built-in functions implode() and explode() to perform these operations. As the name suggests, the Implode / implode()method concatenates the elements of the array with a string segment that works like glue, and similarly the Explode / explode()method does the exact opposite. that is, given a string and a delimiter, it creates an array of strings delimited by a delimiter. implode() method Syntax:
string implode (glue, pieces) 
string implode (pieces) 
Parameters : The function takes two parameters, as described below.
  • glue (optional):This parameter expects a string segment to be used as glue to merge parts of the array. This is an optional parameter with a default value of an empty string.
  • parts:This parameter expects an array whose elements must be concatenated together to create the final expanded string.
Return type : This function loops through the input array and concatenates each item with one glue segment, splitting them apart to create and return the final imploded string.Below the program illustrates how implode() works in PHP:
// PHP -code to illustrate how implode() works $array1 = array ( 'www' , ' engineerforengineer' , 'org' ); echo (implode ( '.' , $array1 ). " < br > " ); $array2 = array ( ' H' , 'E' , ' L ' , ' L' , 'O' ); echo (implode ( $array2 )); ?>
www.engineerforengineer .org HELLO 
You can refer to the article at PHP | Implode() function, for details on implode(). explosion() method Syntax:
array explode (delimiter, string, limit) 
Parameters : the function takes three parameters, as described below.
  • delimiter:This parameter expects a line segment that can be used as a delimiter. If the delimiter itself is not present in the string, then the result is an array containing the string as the only element.
  • string:this parameter expects a string to be exploded.
  • limit:This parameter expects an integer (both positive and negative). This is an optional parameter with a default value of PHP_INT_MAX. The limit refers to the maximum number of divisions that must be made in the input string.
Return type : This function returns an array of strings containing split segments.The following program illustrates how explode() works in PHP:
// PHP code to illustrate explode() $str1 = '1,2,3,4,5' ; $arr = explode ( ',' , $str1 ); foreach ( $arr as $i ) echo ( $i . '< br >' ); ?>
1 2 3 4 5 
You can refer to the article at PHP | Explode() function, to learn more about Explode(). Important points to note :
  • The implode() function injects array elements and returns the resulting string.
  • Although not recommended, implode() can accept parameters regardless of their order.
  • PHP | Join() is an alias for implode().

Get Solution for free from DataCamp guru