Home PHP String Functions join() PHP join() Function Definition In PHP, join() joins array values into one string with a glue separator. It is an alias of implode().
Glue and array are required in the usual form. Prefer implode() in new code for consistency with the manual.
Syntax Call join with glue and an array. Start with this line.
$csv = join(",", $parts); Call What it does join("-", ["a", "b"])Returns "a-b". implode(",", $parts)Same behavior as join().
join() and implode() are interchangeable. explode() splits a string into an array.
Library declarations join(string $separator, array $array): string Parameters Parameter Description separator Required. The glue inserted between array items. array Required. The array of values to join.
Return Value Return Value Description string A single string made from the array items.
PHP Examples 1. Fruit list 2. vs implode() 3. Path & concat Example 1 — Comma-Separated Fruit List The classic join pattern—readable English for “stick these together.”
<?php
$fruits = ['apple', 'banana', 'orange'];
$result = join(', ', $fruits);
echo $result . "\n";
?> How It Works Start with an array. Three fruit names as values.Pick glue. ", " goes between neighbors.Call join. Returns one string with no trailing separator.Read the name. join describes the action clearly for beginners.Example 2 — join() === implode() Prove the alias: same input, same output, same identity check.
<?php
$tags = ['php', 'mysql', 'html'];
$viaJoin = join(' | ', $tags);
$viaImplode = implode(' | ', $tags);
echo "join(): $viaJoin\n";
echo "implode(): $viaImplode\n";
echo "Same: " . ($viaJoin === $viaImplode ? "yes\n" : "no\n");
?> join(): php | mysql | html
implode(): php | mysql | html
Same: yes How It Works Same glue and array. Both calls use ' | ' and $tags.Same string. Output lines match character for character.=== is true. Not “similar”—identical return values.Pick a convention. Behavior never differs; style might.Example 3 — Path Segments and Empty Glue Join with / for simple URL-like paths, or omit the separator to concatenate.
<?php
$segments = ['api', 'v1', 'users', '42'];
$url = join('/', $segments);
$chars = ['H', 'e', 'l', 'l', 'o'];
$word = join($chars);
echo "URL: /$url\n";
echo "Word: $word\n";
?> URL: /api/v1/users/42
Word: Hello How It Works Slash glue. Segments become a path-style string.Leading slash. Added in echo—not by join itself.One-arg form. join($chars) uses an empty separator.Result. Letters stick together as Hello.Frequently Asked Questions What does PHP join() do? join() joins array elements into one string with a separator between each value. It is an alias of implode()—same parameters, same return value, identical behavior.
What is the syntax for join()? string join(string $separator, array $array). You can also call join(array $array) with one argument to concatenate with no separator. Argument order matches implode() in PHP 8+.
What is the difference between join() and implode()? There is no functional difference. join() is an alias of implode(). implode() is more common in PHP codebases; join() reads naturally in English and matches naming in JavaScript and other languages.
Which should I use—join() or implode()? Either is correct. Many teams standardize on implode() for consistency with PHP documentation examples. Use join() when it reads more clearly in your code—just stay consistent within a project.
What happens when join() receives an empty array? It returns an empty string, same as implode(). A single-element array returns that element with no separator added.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion