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.
Fundamentals
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.
Foundation
📝 Syntax
In PHP 8+, the separator must come before the array:
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.
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.
Applications
🚀 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.
Important
📝 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.
Wrap Up
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.
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)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about implode()
Use these points whenever you join arrays into strings in PHP.
5
Core concepts
🔗01
Array → String
Join with glue.
Purpose
🛠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.