W3docs

join()

The join() function in PHP is used to join the elements of an array into a string. It is also known as the implode() function.

Introduction

The join() function in PHP joins the elements of an array into a single string, placing a separator (often called the "glue") between each element. It is an alias of implode() — the two functions are identical, so you can use whichever name reads better in your code.

This page covers the syntax of join(), both ways of calling it, what happens with mixed-type and nested values, and the common mistakes to avoid.

Syntax

join(string $separator, array $array): string
join(array $array): string
  • $separator — the glue string inserted between each element. If you omit it, an empty string "" is used.
  • $array — the array whose values are joined together. Only the values are used; array keys are ignored.

The function returns the joined string.

Argument order: Because join() is an alias of implode(), the historical "separator first, array second" order is the one you should use. The legacy form where the array came first was removed in PHP 8.0.

Basic example

php— editable, runs on the server

This outputs:

apple, banana, orange

Here join() glues the three values together with ", " between them. The result is stored in $fruit_string and printed with echo.

Joining without a separator

If you call join() with only the array, the elements are concatenated directly with nothing between them:

<?php

$parts = ["2024", "06", "21"];
echo join($parts);        // 20240621
echo "\n";
echo join("-", $parts);   // 2024-06-21

How values are converted

Every value is cast to a string before being joined, following PHP's normal string-conversion rules:

<?php

$mixed = [1, 2.5, true, false, null, "text"];
echo join("|", $mixed);

This outputs:

1|2.5|1|||text

Notice that true becomes "1", while false and null become empty strings — which is why you see consecutive | separators. Be careful joining arrays that may contain booleans or null.

Building CSV-style or HTML output

join() is handy whenever you need to flatten a list into text — query strings, CSV rows, or a list of HTML attributes:

<?php

$row = ["Alice", "[email protected]", "Admin"];
echo join(",", $row);        // Alice,[email protected],Admin
echo "\n";

$classes = ["btn", "btn-primary", "active"];
echo '<button class="' . join(" ", $classes) . '">OK</button>';

Common gotchas

  • Multidimensional arrays don't work. join() only flattens one level. A nested array element converts to the literal string "Array" and emits a warning. Flatten the data first.
  • join() is the inverse of explode(). Use explode() to split a string back into an array, and join()/implode() to put it back together.
  • Keys are dropped. Both numeric and string keys are ignored; only values are joined, in their internal order.
  • implode() — identical function under its primary name.
  • explode() — split a string into an array (the opposite operation).
  • str_split() — split a string into chunks of a fixed length.

Practice

Practice
What is the primary function of the implode() function in PHP?
What is the primary function of the implode() function in PHP?
Was this page helpful?