PHP implode() Function
Example
Combine array elements into a string:
<?php $arr = array('Hello','World!','I','love','Shanghai!'); echo implode(" ",$arr); ?>
Definition and Usage
The implode() function returns a string composed of array elements.
Note:The implode() function accepts two parameter orders. However, due to historical reasons, explode() is not, you must ensure that separator Parameter in string .
Note:Parameters of the implode() function must be specified before the separator The parameter is optional. However, for backward compatibility, it is recommended that you use two parameters.
Note:This function is binary safe.
Syntax
implode(separator,array)
Parameter | Description |
---|---|
separator | Optional. Specifies the content placed between array elements. The default is "" (empty string). |
array | Required. The array to be combined into a string. |
Technical Details
Return Value: | Returns a string composed of array elements. |
PHP Version: | 4+ |
Update Log: | In PHP 4.3.0,separator Parameters become optional. |
More Examples
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); ?>