join
Joins array elements into one string separated by a given string; an alias of implode().
join(string $separator, array $array): string- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$separatoroptional | string= "" | The string placed between elements. Optional: the one-argument form join($array) is also accepted and uses an empty string. |
$array | array | The array of values to join. Keys are ignored; only values are used, in the array's current order. |
Return value
stringReturns a string of all the array's values in order, with the separator between each pair. An empty array returns an empty string.
Errors
Throws a TypeError if the arguments are given in the old join($array, $separator) order, and an Error if an element is an object without __toString().
Changelog
- PHP 8.0.0Passing the separator after the array is no longer supported and raises a TypeError.
- PHP 7.4.0Passing the separator after the array (the legacy signature) was deprecated.
join() is an alias of implode(): it glues an array's values into a single string, placing the separator between elements and never before the first or after the last.
It is the right tool for building a CSV line, a comma-separated tag list, an SQL placeholder list like ?,?,?, a URL path from segments, or any "print these values on one line" job. The separator is technically optional — join(['a','b','c']) gives "abc" — but pass it explicitly, because an accidentally omitted separator produces a plausible-looking wrong answer rather than an error.
How values are converted
Each element goes through the usual string conversion, which surprises people: true becomes "1", while false and null both become an empty string, so you get consecutive separators where you may have expected the word. Floats drop trailing zeros. A nested array raises an "Array to string conversion" warning and contributes the literal text Array, and an object without __toString() raises an Error. When formatting matters, map the values first.
Keys are ignored entirely, so joining an associative array gives you the values only. Note also that PHP 8.0 removed the ancient join($array, $glue) argument order; legacy code using it now throws a TypeError.
Example
When to use it
- Build a CSV line from a row array before writing it to a file.
- Turn a list of tag names into a comma-separated string for display.
- Assemble an SQL placeholder list such as ?,?,? sized to an array of bound values.
- Join URL path segments into a single route string.
Frequently asked questions
What is the difference between join() and implode()?
None — join() is a registered alias of implode() with identical behaviour. implode() is the canonical name and the one most style guides expect.
How do I join an associative array with join()?
join() outputs values and ignores keys, so combine it with array_map() over array_keys() to build "key=value" strings first, or use http_build_query() when you want URL-encoded pairs.
Why does join() say "Array to string conversion"?
Because one of the elements is itself an array, and join() does not recurse. Flatten the structure first — for example with array_merge(...array_values($arr)) — or map each sub-array through join().
More examples
Formatting values before joining
Prints "Lovelace, Ada",[email protected],1815. join() itself does no quoting, so the array_map() pass wraps any field containing the separator in quotes first. Without it the CSV line would appear to have four columns.
<?php
$row = ['Lovelace, Ada', '[email protected]', '1815'];
$line = join(',', array_map(
fn(string $f): string => str_contains($f, ',') ? '"' . $f . '"' : $f,
$row
));
echo $line, "\n";How non-strings are converted
The first line prints 1|2.5|1|||x: the float loses its trailing zero, true becomes "1", and false and null both become empty strings, producing the run of separators. The second prints a|Array — a nested array is not flattened, it stringifies to the literal word.
<?php
echo join('|', [1, 2.50, true, false, null, 'x']), "\n";
$nested = ['a', ['b', 'c']];
echo @join('|', $nested), "\n";Placeholders, and keys being ignored
The first part builds "?,?,?" sized to the array, the standard way to bind a variable-length IN list safely. The last two lines print one-two and a-b, showing that join() reads values only — to include keys you have to pull them out yourself.
<?php
$ids = [11, 12, 13];
$placeholders = join(',', array_fill(0, count($ids), '?'));
echo "SELECT * FROM users WHERE id IN ($placeholders)\n";
$assoc = ['a' => 'one', 'b' => 'two'];
echo join('-', $assoc), "\n";
echo join('-', array_keys($assoc)), "\n";
Discussion