PHP implode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, implode() joins array values into one string, inserting a glue string between items.

Glue and array are required in the usual form. join() is an alias of implode().

Syntax

Call implode with glue and an array. Start with this line.

PHP
$csv = implode(",", $parts);
CallWhat it does
implode(",", ["a", "b", "c"])Returns "a,b,c".
implode(" ", $words)Joins words with spaces.

Argument order is glue first, then array. explode() splits a string back into pieces.

Library declarations
PHP
implode(string $separator, array $array): string

Parameters

ParameterDescription
separatorRequired. The glue inserted between array items.
arrayRequired. The array of values to join.

Return Value

Return ValueDescription
stringA single string made from the array items.

PHP Examples

Example 1 — Comma-Separated List

The classic use: turn an array into a readable list.

PHP
<?php
$fields = ['lastname', 'email', 'phone'];
$result = implode(', ', $fields);

echo "Result: $result\n";

$items = ['PHP', 'MySQL', 'JavaScript'];
$html  = "<ul><li>" . implode("</li><li>", $items) . "</li></ul>";
echo $html . "\n";
?>

How It Works

  1. Pick glue. ", " goes between field names.
  2. Join values. No separator after the last item.
  3. HTML trick. Glue </li><li> sits between list items.
  4. Wrap ends. Add opening/closing <ul>/<li> around the joined middle.

Example 2 — Empty Array, One Element, Assoc Values

Edge cases and the “keys are ignored” rule.

PHP
<?php
$many  = ['a', 'b', 'c'];
$one   = ['solo'];
$empty = [];
$assoc = ['first' => 'four', 'five', 'third' => 'six'];

echo "Many:  '" . implode('|', $many) . "'\n";
echo "One:   '" . implode('|', $one) . "'\n";
echo "Empty: '" . implode('|', $empty) . "'\n";
echo "Assoc: '" . implode(',', $assoc) . "'\n";
?>

How It Works

  1. Many items. Glue appears only between neighbors.
  2. One item. Returns that value alone—no |.
  3. Empty. Returns "", not null or false.
  4. Assoc arrays. Keys like first are skipped; values join in order.

Example 3 — Roundtrip with explode()

Split, edit, join again—common with tags or CSV-like settings. See explode().

PHP
<?php
$original = "apple,banana,cherry";
$parts    = explode(",", $original);
$parts[]  = "date";
$joined   = implode(",", $parts);

echo "Original: $original\n";
echo "Updated:  $joined\n";
echo "join():   " . join(",", $parts) . "\n";
?>

How It Works

  1. Split. explode(",", $original) builds an array of fruits.
  2. Edit. Append "date" to the array.
  3. Join. implode(",", $parts) rebuilds the CSV string.
  4. Alias check. join() produces the same string as implode().

Frequently Asked Questions

implode() joins array elements into one string, placing a separator (glue) between each value. For example, implode(', ', ['a', 'b', 'c']) returns "a, b, c".
string implode(string $separator, array $array). The separator comes first, then the array. The old reversed argument order (array first) was removed in PHP 8.0.
implode() joins array elements into a string (array → string). explode() splits a string into an array using a delimiter (string → array). They are conceptual opposites for simple delimited text.
Yes. join() is an alias of implode()—same parameters, same result. implode() is more common in PHP code; join() reads naturally in English.
It returns an empty string "". With one element, it returns that element with no separator added.

Next: join()

Same function under a clearer English name.

join() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful