PHP implode() Function | How to use implode in PHP?
Summary: In this article, we demonstrate and explore PHP implode() Function and how to use implode in PHP? Let’s understand in detail this Function with an example.
PHP implode() Function Definition and Usage
The implode function is used to “join elements of an array with a string”. The implode() function in PHP returns a string from the elements of an array. furthermore, it takes an array of strings and joins them together into one string using a delimiter (i.e. string to be used between the pieces) of your preference.
This function, can be remembered as “array to string”, therefore, which means that it takes an array and returns a string. furthermore, it rejoins any array elements and it will return the resulting string, which may be put in a variable.
The implode() function in PHP, accepts its parameters in either order. However, for consistency with explode(), you must use the documented order of arguments.
The separator parameter of implode() is optional. However, it’s suggested to always use two parameters for backward compatibility.
This function is binary-safe.
The separator parameter of implode() is optional. However, it is suggested to always use two parameters for backward compatibility.
Syntax:
Parameter Value:
The implode() function in PHP, accepts two parameters out of which one is an optional parameter and one is mandatory.
Parameter Value | Description |
---|---|
separator: | It is an optional parameter. It specifies what to put between the array elements which means that the values of the array will be joined to form a string and it will be separated by the separator parameter provided here. furthermore, if not provided the default is “”( which means an empty string). |
array: | It is a required parameter. The array whose value is to be joined to form a string. |
Return Type:
The return type of implode() function in PHP is a string. It will return the joined string formed from the elements of the array.
Suppose that you have an array such as this:-
1 |
$myarr = Array ("W","E","L",C","O","M","E"); |
and you would like to combine it into a string, by putting the separator ‘-‘ between every element of the array.How to do that?
1 |
$mystr = implode("-",$myarr); |
So your resulting string variable $mystr will contain:
So during this article, you saw a way to use the implode and explode functions in PHP.
PHP Implode() Example
Program 1: Join array elements with a string
1 2 3 4 |
<?php $myarr = array('Hello','princes!','Enjoy','beautiful','Day!'); echo implode(" ",$myarr); ?> |
Program 2: Separate the array elements with different characters:
1 2 3 4 5 6 7 |
<?php $myarr = array('Hello','princes!','Enjoy','beautiful','Day!'); echo implode(" ",$myarr)."<br>"; echo implode("+",$myarr)."<br>"; echo implode("-",$myarr)."<br>"; echo implode("X",$myarr); ?> |
Hello+princes!+Enjoy+beautiful+Day!
Hello-princes!-Enjoy-beautiful-Day!
HelloXprinces!XEnjoyXbeautifulXDay!
Program 3: An array with one or no elements
1 2 3 4 5 6 7 8 |
<?php $x1 = array("21","22","23"); $x2 = array("c"); $x3 = array(); echo "x1 is: '".implode("','",$x1)."'<br>"; echo "x2 is: '".implode("','",$x2)."'<br>"; echo "x3 is: '".implode("','",$x3)."'<br>"; ?> |
x2 is: ‘c’
x3 is: ”
Conclusion:
In this article, you have learned to implode() Function and the way to use implode in PHP. I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!