Home >Backend Development >PHP Problem >How to use php implode function?
php The implode function is used to return a string composed of array elements. Its syntax is implode(separator,array). The parameter array is required and refers to the array to be combined into a string.
#How to use the php implode function?
Definition and usage
implode() function returns a string composed of array elements.
Note: The implode() function accepts two parameter orders. However, due to historical reasons, explode() does not work. You must ensure that the separator parameter comes before the string parameter.
Note: The separator parameter of the implode() function is optional. However, for backward compatibility, it is recommended that you use two parameters.
Note: This function is binary safe.
Syntax
implode(separator,array)
Parameters
separator Optional. Specifies what is placed between array elements. Default is "" (empty string).
array Required. Arrays to be combined into strings.
Return value: Returns a string composed of array elements.
PHP Version: 4
Change Log: In PHP 4.3.0, the separator parameter becomes optional.
Example 1
Separate array elements with different characters:
<?php $arr = array('Hello','World!','I','love','Shanghai!'); echo implode(" ",$arr)."<br>"; echo implode("+",$arr)."<br>"; echo implode("-",$arr)."<br>"; echo implode("X",$arr); ?>
Output:
Hello World! I love Shanghai! Hello+World!+I+love+Shanghai! Hello-World!-I-love-Shanghai! HelloXWorld!XIXloveXShanghai!
Example 2
Combine array elements into strings:
<?php $arr = array('Hello','World!','I','love','Shanghai!'); echo implode(" ",$arr); ?>
Output:
Hello World! I love Shanghai!
The above is the detailed content of How to use php implode function?. For more information, please follow other related articles on the PHP Chinese website!