Signature
join(string $separator, array $array): string
join(array $array): stringPurpose
join() turns the elements of an array into one string, placing a separator between neighbouring elements. It is an alias of implode(): the two names call the same function and behave identically. Pick whichever name your team prefers and use it consistently, because mixing both in one codebase makes searching harder.
Typical uses are building a comma-separated list for display, creating a slug or path from parts, and assembling a short log line from several values.
Signature
Because join() is an alias, it accepts the same call forms as implode().
Parameters and defaults
$separator: the text inserted between elements. It can be any string, including a multi-character one such as" | ". When you use the single-argument form, the separator is an empty string, so elements are simply concatenated.$array: the array whose values are joined. Values are taken in the array's iteration order. Keys are never included in the result.
The separator comes first, then the array. If you pass them in the opposite order (array first, separator second), PHP 8.4 raises a TypeError because the arguments no longer match the accepted forms.
Return value and failure cases
The function always returns a string. An empty array produces an empty string, and a one-element array produces that element converted to a string, with no separator.
Each element is converted to a string before joining:
- integers and floats become their usual text form, for example
2.5becomes"2.5"; truebecomes"1", whilefalseandnullbecome"";- objects are accepted only if they implement
__toString(); otherwise PHP throws anError; - a nested array cannot be converted meaningfully. It becomes the text
Arrayand PHP emits a warning, so flatten nested data first.
Example: a readable list
The most common case is a list of plain strings with a human-friendly separator.
$tags = ['php', 'strings', 'arrays'];
echo join(', ', $tags), "\n";php, strings, arrays
The output keeps the array order and adds the separator only between items, never at the start or the end.
Example: scalar conversion
Mixed scalar values are converted one by one. Notice that false and null leave an empty slot, but the separators around them are still printed.
$values = [1, 2.5, true, false, null, 'x'];
echo json_encode(join('|', $values)), "\n";"1|2.5|1|||x"
Using json_encode() around the result makes the exact string visible, including the adjacent separators.
Example: empty input, no separator and keys
This example shows three edge cases together: an empty array, the single-argument form, and an associative array.
$empty = join(',', []);
$noSeparator = join(['a', 'b', 'c']);
$assoc = join('-', ['x' => 'one', 'y' => 'two']);
echo json_encode([$empty, $noSeparator, $assoc]), "\n";["","abc","one-two"]
The empty array gives an empty string, the one-argument form glues values directly together, and the keys x and y do not appear in the result.
Edge cases
- No trailing separator. The separator appears only between elements, so you never need to trim a final comma.
- Empty strings count as elements.
join(',', ['a', '', 'b'])yieldsa,,b. If you want to skip blanks, filter the array before joining. - Order follows the array. Sort first if you need a particular order in the output.
- Separators are literal. Characters in the separator are not treated as patterns, and nothing is escaped. If the elements themselves contain the separator, the result cannot be split back reliably.
Common mistakes
- Swapping the arguments. Remember the order: separator, then array.
- Expecting keys in the output. Only values are joined. Build the strings yourself, for example with
array_map(), if keys must be included. - Joining nested arrays. The inner array becomes
Arrayand triggers a warning. Flatten or format inner arrays before the call. - Building CSV by hand. Joining with a comma does not quote fields that contain commas, quotes or newlines. Use a CSV-aware function for real CSV output.
- Joining objects without
__toString(). This throws an error instead of producing a fallback text. - Treating
falseornullas visible text. They disappear as empty strings, which can make a list look as if values are missing.
Related functions
implode(): the primary name of the same function.explode(): the opposite operation, splitting a string into an array by a delimiter.array_map(): transform elements before joining them.array_filter(): drop unwanted elements such as empty strings first.str_repeat()andsprintf(): other tools for assembling strings from parts.
Recap
join() is an alias of implode(). Call it with a separator and an array, or with an array alone, and it returns one string with the separator between values. Scalars are converted to text, keys are ignored, empty arrays give an empty string, and nested arrays or non-stringable objects are problems you must handle before joining.
Tested examples
$tags = ['php', 'strings', 'arrays'];
echo join(', ', $tags), "\n";php, strings, arrays
$values = [1, 2.5, true, false, null, 'x'];
echo json_encode(join('|', $values)), "\n";"1|2.5|1|||x"
$empty = join(',', []);
$noSeparator = join(['a', 'b', 'c']);
$assoc = join('-', ['x' => 'one', 'y' => 'two']);
echo json_encode([$empty, $noSeparator, $assoc]), "\n";["","abc","one-two"]