W3docs

implode()

Our article is about the PHP function implode(), which is used to join array elements with a string. This function is useful for working with arrays and strings

PHP's implode() joins the elements of an array into a single string, placing a separator (the glue) between each element. It's the function you reach for whenever you have a list of values and need one string — a comma-separated list, a URL path, a CSV row, or a SQL IN (...) clause. It is the exact inverse of explode(), which splits a string back into an array.

This page covers the syntax, the return value, and the cases that trip people up: the optional glue, associative arrays, empty arrays, and how non-string values are converted.

Syntax

implode(string $separator, array $array): string
implode(array $array): string   // legacy: separator defaults to ""
ParameterDescription
$separatorThe string placed between each element. Often called the glue.
$arrayThe array of values to join.

Return value: a single string containing all array elements joined by $separator, in their existing order.

The function never changes the original array — it returns a new string.

A basic example

php— editable, runs on the server

Each element is joined with a single space, producing:

Hello World !

Using a different separator

The glue can be any string, not just a single character. Here we join with a hyphen:

php— editable, runs on the server

Output:

Hello-World-!

A common real-world use is building a comma-separated list for display or for a query:

<?php
$ids = [4, 7, 12, 19];
echo 'WHERE id IN (' . implode(', ', $ids) . ')';
// WHERE id IN (4, 7, 12, 19)
?>

Joining an associative array

implode() ignores the keys entirely and joins only the values, in insertion order:

<?php
$user = ['id' => 1, 'name' => 'Sam', 'role' => 'admin'];
echo implode(' | ', $user);
?>

Output:

1 | Sam | admin

If you need the keys too (for example id=1&name=Sam), use http_build_query() or array_map() over the keys instead — implode() alone cannot see them.

Edge cases and gotchas

Empty array. Joining an empty array always returns an empty string, no matter the separator:

<?php
echo '[' . implode(', ', []) . ']';   // []
?>

Non-string values are coerced to strings. Numbers, booleans, and null are converted using PHP's normal string rules — which can surprise you: true becomes "1", while false and null become "" (an empty string):

<?php
echo implode(', ', [1, 2.5, true, null, 'x']);
// 1, 2.5, 1, , x
?>

If you want null/false filtered out, run the array through array_filter() before joining.

The separator is optional. Calling implode($array) with no glue concatenates the values directly. Passing the separator first is recommended for readability:

<?php
echo implode(['a', 'b', 'c']);   // abc
?>

Before PHP 8.0 the two arguments could be given in either order (implode($array, $glue)). That historical behavior was removed in PHP 8.0 — always pass the separator first.

implode() vs explode()

implode() and explode() are mirror operations: one builds a string from an array, the other rebuilds the array from the string.

<?php
$csv  = implode(',', ['red', 'green', 'blue']); // "red,green,blue"
$back = explode(',', $csv);                      // ['red', 'green', 'blue']
?>

join() is simply an alias of implode() — the two are interchangeable. See join() and explode() for more.

Practice

Practice
What is the purpose of the implode() function in PHP?
What is the purpose of the implode() function in PHP?
Was this page helpful?