implode
Joins array elements into a string.
Available since: PHP 4
Syntax
implode(string $separator, array $array): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $separator | string | The string placed between the elements. |
| $array | array | The array whose elements are joined. |
Return value
stringA string of the array elements separated by `$separator`.
Examples
A comma-separated list:
<?php
echo implode(', ', ['red', 'green', 'blue']);Output:
red, green, blueJoining without a separator, and with numbers:
<?php
echo implode('', ['P', 'H', 'P']), "\n";
echo implode(' + ', [1, 2, 3]);Output:
PHP
1 + 2 + 3Notes
Numbers and other scalar values are converted to strings automatically. explode() does the reverse.
See also
- explode — Splits a string into an array by a separator.
Related recipes
In the tutorial
- Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.