Joining with implode()
Learn to join an array into a string with a delimiter using implode().
Joining with implode()
To join an array into a single string, use implode(). It is called as implode($glue, $array); $glue is the separator placed between pieces, $array is the array to join. The result is a string.
Example: implode('-', ['2025', '01', '15']) returns '2025-01-15'. It is the reverse of explode(): you give an array and get a string.
Joining without a glue
The first argument of implode() is optional; if you omit it, the default glue is an empty string. So implode($array) joins all elements with nothing between them.
Example: implode(['a', 'b', 'c']) returns 'abc'. This is handy when you stitch token codes into a single cell or rebuild a string from its letters. For consistency, writing the glue explicitly is still a common choice.
Exercise Build a file path
Assign the array ['storage', 'uploads', 'avatar.png'] to $parts. Use implode() with the '/' glue to assign the joined string to $path. Then print it with echo $path;.
Show example solution
First assign the $parts array, then write $path = implode("/", $parts); and finally echo $path;
Example solution:
<?php
$parts = ["storage", "uploads", "avatar.png"];
$path = implode("/", $parts);
echo $path;
Test yourself
-
What does implode('-', ['2025', '01', '15']) return?
implode('-', ['2025', '01', '15'])- 2025 01 15
- 2025-01-15
- 20250115
- ['2025-01-15']
Show answer
2025-01-15 — Correct. The three pieces join with '-': '2025-01-15'.
-
Join the tags
$tags = ['php', 'web', 'backend']; $label = ____(', ', $tags); echo $label;Show answer
$tags = ['php', 'web', 'backend']; $label = implode(', ', $tags); echo $label; -
Match each call to its result
- implode('-', ['a','b'])
- a-b
- implode('', ['x','y','z'])
- xyz
- implode(', ', ['1','2'])
- 1, 2