implode($separator, $array) joins the values of an array into one string, with $separator between each pair: implode(', ', ['red', 'green', 'blue']) returns "red, green, blue".
The separator only goes between elements, never before the first or after the last. It is the reverse of explode(), which splits a string into an array.
Syntax
implode(string $separator, array $array): string
implode(array $array): string // separator defaults to ""
The separator comes first. Older code sometimes passed the array first, implode($array, ', '). That order was deprecated in PHP 7.4 and removed in PHP 8.0, where it throws TypeError: implode(): Argument #2 ($array) must be of type ?array, string given.
implode vs join
join() is an alias of implode(). They take the same arguments and return the same string, so pick one and stay consistent.
What happens to numbers, booleans and null
Every value is converted to a string first. Numbers print as you would expect, true becomes "1", and false and null become empty strings, which leaves two separators in a row.
Implode an associative array
implode() ignores keys and joins only the values. To get key=value pairs, build each piece with array_map() over the keys and values, then join. http_build_query() does the same for a URL query string and also encodes spaces and special characters.
The same pattern joins only the keys: implode(', ', array_keys($user)). More on keys and values is on the associative arrays page.
Join with a newline or build an HTML list
The separator can be any string. "\n" (double quotes) puts each value on its own line, which is handy for text files and command line output. With HTML tags as the separator, implode() builds a list in one line. Run the next block and the Page tab shows the rendered list.
Escaping each value with htmlspecialchars() before joining keeps a value like <b> from becoming markup.
Join with "and" before the last item
A human-readable list usually needs "and" before the last element. Pop it off, join the rest, then add it back:
Common mistake: nested arrays
implode() works on one level. An element that is itself an array is converted to the word Array, and PHP prints a warning:
<?php
echo implode(', ', ['red', ['orange', 'yellow']]);
Warning: Array to string conversion in /home/index.php on line 2
red, Array
Flatten the inner arrays first, or join each one separately:
Frequently Asked Questions
What does implode() do in PHP?
It joins the values of an array into one string, putting the separator between them: implode(', ', ['a', 'b', 'c']) returns "a, b, c". It is the opposite of explode().
What is the difference between implode() and join()?
There is none. join() is an alias of implode(): same arguments, same result. implode() is the name you will see in most code.
How do I implode an associative array with its keys?
implode() uses only the values. To include keys, build the pieces first: implode(', ', array_map(fn($k, $v) => "$k=$v", array_keys($a), $a)). For a URL query string, http_build_query($a) does it in one call.
Why does implode() give a TypeError in PHP 8?
Because the arguments are in the old order. implode($array, ', ') was deprecated in PHP 7.4 and removed in PHP 8.0. Write the separator first: implode(', ', $array).
How do I convert an array to a comma separated string in PHP?
implode(',', $array). Use ', ' as the separator if you want a space after each comma. For a CSV file, use fputcsv(), which also quotes values that contain commas.