implode

Joins the elements of an array into a single string, optionally separated by a given string.

Signature
implode(string $separator, array $array): string
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$separatorstringThe string placed between elements. In the alternative one-argument form implode(array $array) it is omitted and defaults to an empty string.
$arrayarrayThe array of values to join.

Return value

stringA string containing every array element in order, with the separator between each pair. An empty array returns an empty string.

Errors

Emits an E_WARNING ("Array to string conversion") for any element that is itself an array, and throws a TypeError if an element is an object without __toString.

Changelog

  • PHP 8.0.0Passing the separator after the array is no longer supported and raises a TypeError.
  • PHP 7.4.0Passing the separator after the array was deprecated.

implode flattens an array into one string, inserting the separator between elements but never before the first or after the last. join is an alias with identical behaviour. An empty array gives an empty string; a one-element array gives that element with no separator at all.

When to reach for it

Anywhere a list becomes text: building a comma-separated display of tags, generating ? placeholders for a prepared statement, assembling a path from segments, or joining lines before writing a file. Combine it with array_map when each element needs formatting first, and with array_filter to drop empty values so you do not end up with doubled separators.

What trips people up

The legacy argument order — array first, separator second — was deprecated in PHP 7.4 and removed in 8.0. Old code that calls implode($parts, ", ") now throws a TypeError.

Values are cast to string with PHP's normal rules, and that is rarely what people expect: true becomes "1" while false and null both become "", so a boolean array joins into a run of separators. A nested array becomes the literal text Array plus a warning, and an object without __toString throws.

Keys are ignored entirely — only values are joined, in insertion order.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Turn a tag or category array into a comma-separated line for display or a CSV cell.
  • Build the ?,?,? placeholder list for an SQL IN clause from a value array.
  • Join path or namespace segments back together after processing them as an array.

Frequently asked questions

What is the difference between implode and join?

None — join is an alias of implode with the same signature and behaviour. implode is the canonical name and the one most codebases use.

Why do I get a TypeError from implode after upgrading to PHP 8?

You are passing the arguments in the old order, array first and separator second. That form was deprecated in 7.4 and removed in 8.0; swap them to implode($separator, $array).

Does implode use array keys?

No, only values are joined, in the array's insertion order. To include keys, map the array into "key=value" strings first, or use http_build_query.

More examples

Building SQL placeholders

array_fill produces one placeholder per value and implode joins them, so the IN clause always matches the number of bound parameters. The second call maps column names into assignments before joining, which is the usual way to build a SET list.

Example · php
<?php
$ids = [4, 8, 15, 16];

$holders = implode(",", array_fill(0, count($ids), "?"));
echo "SELECT * FROM t WHERE id IN ($holders)\n";

$named = implode(", ", array_map(fn($k) => "$k = ?", ["name", "email"]));
echo "UPDATE users SET $named WHERE id = ?\n";

How values are cast

The first result is "1|1|||2.5|x": true prints as 1 while false and null vanish, leaving empty slots. Filtering them out first removes the gaps, and mapping through var_export shows the real values when you need a debug dump rather than display text.

Example · php
<?php
$mixed = [1, true, false, null, 2.5, "x"];

var_dump(implode("|", $mixed));
var_dump(implode("|", array_filter($mixed, fn($v) => $v !== null && $v !== false)));
var_dump(implode("|", array_map(fn($v) => var_export($v, true), $mixed)));

Round trip with explode

explode and implode with the same separator are exact inverses, which the bool(true) confirms. Slicing the array before joining is a clean way to drop the last path segment without any string arithmetic.

Example · php
<?php
$line = "usr/local/bin";

$segments = explode("/", $line);
print_r($segments);

$rebuilt = implode("/", $segments);
var_dump($rebuilt === $line);
echo implode("/", array_slice($segments, 0, -1)), "\n";

Discussion

  • Be the first to comment on this lesson.