Strings7 min readExamples run on PHP 8.5.10

join()

Learn how PHP join() works as an alias of implode(): its signature, how values are converted, edge cases with empty arrays and keys, and mistakes to avoid.

Signature

join(string $separator, array $array): string
join(array $array): string

Purpose

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.5 becomes "2.5";
  • true becomes "1", while false and null become "";
  • objects are accepted only if they implement __toString(); otherwise PHP throws an Error;
  • a nested array cannot be converted meaningfully. It becomes the text Array and 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.

Example
<?php
$tags = ['php', 'strings', 'arrays'];
echo join(', ', $tags), "\n";
Output
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.

Example
<?php
$values = [1, 2.5, true, false, null, 'x'];
echo json_encode(join('|', $values)), "\n";
Output
"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.

Example
<?php
$empty = join(',', []);
$noSeparator = join(['a', 'b', 'c']);
$assoc = join('-', ['x' => 'one', 'y' => 'two']);
echo json_encode([$empty, $noSeparator, $assoc]), "\n";
Output
["","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']) yields a,,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

  1. Swapping the arguments. Remember the order: separator, then array.
  2. Expecting keys in the output. Only values are joined. Build the strings yourself, for example with array_map(), if keys must be included.
  3. Joining nested arrays. The inner array becomes Array and triggers a warning. Flatten or format inner arrays before the call.
  4. 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.
  5. Joining objects without __toString(). This throws an error instead of producing a fallback text.
  6. Treating false or null as visible text. They disappear as empty strings, which can make a list look as if values are missing.
  • 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() and sprintf(): 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

Join a list with a readable separator
<?php
$tags = ['php', 'strings', 'arrays'];
echo join(', ', $tags), "\n";
Output
php, strings, arrays
Scalar conversion of mixed values
<?php
$values = [1, 2.5, true, false, null, 'x'];
echo json_encode(join('|', $values)), "\n";
Output
"1|2.5|1|||x"
Empty array, single argument and associative keys
<?php
$empty = join(',', []);
$noSeparator = join(['a', 'b', 'c']);
$assoc = join('-', ['x' => 'one', 'y' => 'two']);
echo json_encode([$empty, $noSeparator, $assoc]), "\n";
Output
["","abc","one-two"]