PHP implode() Function

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 5 Examples
String Functions

What You’ll Learn

The implode() function joins array elements into a single string with a separator (glue) between each value. It is one of the most common ways to turn lists into readable text, CSV-style output, or HTML fragments.

01

Array → String

Join with glue.

02

Separator

Comma, space, <br>.

03

vs explode()

Join vs split.

04

join() Alias

Same function.

05

Values Only

Keys ignored.

06

PHP 8 Order

Glue first, then array.

Definition and Usage

In PHP, implode() concatenates all elements of an array in order, inserting a separator string between each pair of values. The result is always a string—even when the input array is empty (you get ""). Non-string values are cast to string automatically (booleans become 1 or empty, numbers become digit strings).

💡
Beginner Tip

Think of implode() as the opposite of explode() for simple cases: explode(',', 'a,b,c') makes an array; implode(',', ['a','b','c']) makes a,b,c. For SQL IN (...) lists, prefer prepared statements—not raw implode of user input.

📝 Syntax

In PHP 8+, the separator must come before the array:

PHP
string implode(string $separator, array $array)

// Optional: omit separator (defaults to "")
string implode(array $array)

Parameters

  • $separator — glue placed between elements (e.g. ", ", "\n", ""). Optional when using the one-argument form.
  • $array — list of values to join. Keys are ignored; only values are used.

Return Value

Returns a string containing all array values joined with the separator between them.

⚡ Quick Reference

Comma list
implode(", ", $arr)

Readable CSV-style

No separator
implode($arr)

Concatenate values

Newlines
implode("\n", $lines)

Multiline text

Split reverse
explode(",", $str)

String → array

Examples Gallery

Run these examples in PHP 7+. Each demonstrates a common pattern beginners encounter with implode().

📚 Getting Started

Join field names or tags with a comma and space.

Example 1 — Comma-Separated Field List

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

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

echo "Result: $result\n";
?>

How It Works

PHP inserts ", " between each element. There is no trailing separator after the last item.

Example 2 — Build an HTML Unordered List

Use implode with HTML tags as glue to wrap each item in <li>.

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

echo $html . "\n";
?>

How It Works

The glue </li><li> sits between values; you wrap the result with opening and closing list tags. Escape user content with htmlspecialchars() when items are not trusted.

📈 Practical Patterns

Edge cases, associative arrays, and explode roundtrips.

Example 3 — Empty Array and Single Element

Know what implode returns for arrays with zero or one item.

PHP
<?php
$many   = ['a', 'b', 'c'];
$one    = ['solo'];
$empty  = [];

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

How It Works

A single element returns that value alone with no separator. An empty array returns an empty string—not false or null.

Example 4 — Associative Arrays Use Values Only

Keys are ignored; only values are joined in internal order.

PHP
<?php
$indexed = ['one', 'two', 'three'];
$assoc   = ['first' => 'four', 'five', 'third' => 'six'];

echo implode(',', $indexed) . "\n";
echo implode(',', $assoc) . "\n";
?>

How It Works

To include keys in the output (e.g. name=Tom), build key-value pairs manually or use http_build_query() for URL-style strings.

Example 5 — Roundtrip with explode()

Split a string into an array, modify it, then join it back.

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

echo "Original: $original\n";
echo "Updated:  $joined\n";
?>

How It Works

explode() breaks the string on commas; after adding an element, implode() rebuilds the list. This pattern is common for editing comma-separated settings or tags.

🚀 Common Use Cases

  • Display lists — show tags, categories, or names as comma-separated text.
  • Build HTML fragments — join rows or list items with markup between values.
  • Log messages — combine debug values with a delimiter for readable logs.
  • File paths / URLs — join path segments (often DIRECTORY_SEPARATOR is clearer for paths).
  • Reassemble after explode() — edit delimited data in array form, then join again.

🧠 How implode() Works

1

You pass glue and array

Separator string (or omit for empty glue) plus an array of values to join.

Input
2

Values are cast to strings

Each element becomes a string. Keys are skipped; order follows the array’s internal value order.

Transform
3

Glue inserted between items

PHP concatenates value + glue + value + glue … with no glue after the last value.

Join
=

Single string

Ready to echo, store, or pass to another function.

📝 Notes

  • join() is an alias—identical behavior to implode().
  • PHP 8.0 removed the legacy argument order implode($array, $separator).
  • Passing a string instead of an array returns null and emits a warning (PHP 8+).
  • Empty array → empty string; one element → that element alone.
  • For SQL IN (...) clauses, use prepared statements with bound parameters—not raw implode of user IDs.

Conclusion

The implode() function turns arrays into delimited strings efficiently. Master the separator-first syntax, remember that keys are ignored, and pair it with explode() when you need to split and rejoin text.

Use it for lists, logs, and HTML snippets—and reach for join() when that name reads better in your code.

💡 Best Practices

✅ Do

  • Use implode($separator, $array) order in PHP 8+
  • Escape HTML when joining user content into markup
  • Use explode() + edit + implode() for simple CSV-style data
  • Consider http_build_query() for URL query strings
  • Filter null values with array_filter() if empty slots matter

❌ Don’t

  • Pass user input directly into SQL via implode without binding
  • Assume associative array keys appear in the output
  • Use reversed argument order from PHP 7 legacy code on PHP 8
  • Pass a string where an array is expected
  • Confuse implode with string concatenation in loops (implode is faster for many items)

Key Takeaways

Knowledge Unlocked

Five things to remember about implode()

Use these points whenever you join arrays into strings in PHP.

5
Core concepts
🛠 02

Glue First

PHP 8 argument order.

Syntax
🔄 03

vs explode()

Split vs join.

Related
📝 04

Values Only

Keys ignored.

Arrays
🔗 05

join() Alias

Same function.

Alias

❓ 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.
No. implode() works on array values only and ignores keys. For an associative array ['a' => 1, 'b' => 2], implode(',', $arr) joins the values (1 and 2), not the keys.

Explore More PHP String Functions

Continue with join(), explode(), and the rest of the string function reference.

Next: join() →

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.

6 people found this page helpful