Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท String Functions

PHP implode() function

Reference for PHP implode() and its alias join(): glue array elements into a string with a separator, how values are converted, and the removed legacy order.

implode() joins the elements of an array into one string, placing a separator between them. It is the inverse of explode() and is used for building comma-separated lists, CSS class names, SQL placeholders and readable messages.

join() is an alias with identical behavior. The array is the last argument when you pass a separator; the separator can also be omitted, in which case elements are simply concatenated.

PHP
<?php
echo implode(", ", ["a", "b", "c"]);  // a, b, c
echo implode(["x", "y"]);             // xy

Try it

PHP Joining values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Practical joins

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
implode(string $separator, array $array): string
implode(array $array): string
Name Description
$separator The text placed between elements. Defaults to an empty string when only the array is given.
$array The array whose values are joined. Keys are ignored.

Return value: the joined string; an empty array gives "".

Errors: a TypeError is thrown for invalid arguments. A nested array element is converted to the string "Array" with a warning.

Usage notes

  • Conversion rules: integers and floats become their string form, true becomes "1", false and null become "". Objects must implement __toString().
  • Legacy argument order removed. Passing the array first and the separator second (implode($array, ",")) was deprecated in PHP 7.4 and removed in PHP 8.0.
  • Only one dimension. Flatten nested arrays first or join the inner arrays separately.
  • Associative arrays: to include keys, build the pairs yourself, for example with array_map() and http_build_query().
  • Filter empty values with array_filter() before joining to avoid stray separators.
  • Large lists are joined faster with implode() than with repeated .= in a loop.