In this article you are going to learn the use of explode() and implode() functions in php. Both of these are string functions and the execution process of both the functions is dependent on a string variable.
They are opposite to each other i.e, they exhibit functionality opposite to each other. While explode() function is used to output an array by breaking apart a string with the help of another string, the implode() function is used to output a string by joining the elements(strings) of an array with the help of another string.
In simple words, the explode() function breaks a string into an array of strings and the implode() function joins an array of strings to form a single string. Join our php training institute for offline classes and learn deeply similar functions and their usage in development of web applications.
To make it more clear, let’s just dive into the concepts one by one with full description about syntax, parameters, return value, example and its output.
EXPLODE() FUNCTION
Syntax : explode($delimiter, $string, $limit);
Three parameters are used in this function which are discussed below.
- $delimiter-it represents a string that helps to split the input string parameter into array of elements(strings).
- $string- it represents the string to be broken into array of elements.
- $limit- it represents an integer value and helps to store the returned array in the following three ways.
(a) if the limit is positive, all the array of strings will be returned in such a way that the first limit element will contain the first string and rest of the strings will be contained in the second limit element.
(b) if the limit is negative, all the array of strings except the last string will be returned.
(c) if the limit is zero, all the array of strings will be returned treating the limit to be 1.
Result/ Return Value : There are three possible outcomes of this function.
- It will return an array of strings on the successful execution of the function.
- It will return an empty array, if the value of delimiter is not present in the string to be split along with the use of a negative limit.
- It will return False, if the delimiter is left empty.
Example : We have shown below an example of explode() function.
Output : You will get the following output of the above php script.
IMPLODE() FUNCTION
Syntax : implode($gum, $array_elements);
Two parameters are used in this function which are discussed below.
- $gum- it represents a string that helps to join the elements(strings) of an array to form a single string.
- $array_elements- it represents the array of strings to be imploded or joined.
Result/ Return Value : The successful execution of the function will return a string value corresponding to the array elements with a gum string in between each element of the output string that is used for imploding or joining array of elements.
Example : An example of implode() function is shown below.
Output : The following output will be generated after running the above php script.
We have tried to put forward the best possible information about the use of explode() and implode() functions in php. Hope you liked this article. For further updates, stay tuned to our php blogs.