PHP join() Function

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 5 Examples
String Functions

What You’ll Learn

The join() function merges array elements into one string with a separator between values. It is an alias of implode()—identical syntax, identical results. Many developers prefer join() because the name reads like plain English.

01

Array → String

Join with glue.

02

implode Alias

Same function.

03

Separator

Comma, dash, newline.

04

Readable Name

“Join these items.”

05

vs explode()

Merge vs split.

06

PHP 8 Order

Glue first, then array.

Definition and Usage

In PHP, join() is a built-in alias for implode(). Both accept a separator string and an array, then return a single string with the separator placed between each element. If you already know implode(), you already know join().

💡
Beginner Tip

Pick one name and use it consistently in your project. PHP docs and most tutorials use implode(); JavaScript uses join(). In PHP, either works—they call the exact same internal function.

📝 Syntax

join() shares the same signatures as implode():

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

// Optional: no separator (defaults to "")
string join(array $array)

Parameters

  • $separator — glue placed between elements (e.g. ", ", " - "). Optional in the one-argument form.
  • $array — values to join. Array keys are ignored; only values are used.

Return Value

Returns a string with all array values joined by the separator. An empty array returns "".

⚡ Quick Reference

Comma list
join(", ", $arr)

Readable list

Same as implode
implode(", ", $arr)

Identical result

Concatenate
join($arr)

No separator

Split reverse
explode(",", $str)

String → array

Examples Gallery

Run these examples in PHP 7+. Each uses join() directly—replace with implode() anytime for the same output.

📚 Getting Started

Join array items into a comma-separated string.

Example 1 — Comma-Separated Fruit List

The classic join pattern from the PHP manual—readable and concise.

PHP
<?php
$fruits = ['apple', 'banana', 'orange'];
$result = join(', ', $fruits);

echo $result . "\n";
?>

How It Works

PHP inserts ", " between each fruit name. The function name join describes exactly what happens: join the array into one string.

Example 2 — join() and implode() Produce Identical Output

Prove the alias relationship with a direct comparison.

PHP
<?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");
?>

How It Works

Both functions call the same underlying code. Choose based on readability and team convention—not behavior.

📈 Practical Patterns

Paths, logs, and concatenation without a separator.

Example 3 — Join Path or URL Segments

Combine parts with a slash separator (for simple cases; use proper path APIs for filesystem paths).

PHP
<?php
$segments = ['api', 'v1', 'users', '42'];
$url      = join('/', $segments);

echo "URL: /$url\n";
?>

How It Works

Each segment is separated by /. For URL query strings, consider http_build_query() instead.

Example 4 — Build a Log Line from Parts

Join timestamp, level, and message with a pipe delimiter.

PHP
<?php
$parts = [date('Y-m-d H:i:s'), 'INFO', 'User logged in'];
$line  = join(' | ', $parts);

echo $line . "\n";
?>

How It Works

Dynamic values (like date()) and static strings are joined in one readable call. The date in output varies at runtime.

Example 5 — Join Without a Separator

Pass only the array to concatenate values with no glue between them.

PHP
<?php
$chars = ['H', 'e', 'l', 'l', 'o'];
$word  = join($chars);

echo $word . "\n";
echo "Length: " . strlen($word) . "\n";
?>

How It Works

The one-argument form uses an empty separator, so characters are glued directly together—equivalent to implode('', $chars) or join('', $chars).

🚀 Common Use Cases

  • Readable list output — comma- or bullet-separated display text.
  • Code that mirrors JavaScript — use join() for familiar naming across languages.
  • Building delimited strings — CSV-style rows, pipe-separated logs, slash-separated paths.
  • Rejoining after explode() — edit array items, then join() back together.
  • Template fragments — combine HTML or text parts with markup as glue.

🧠 How join() Works

1

You call join() or implode()

Both names invoke the same PHP internal function with separator and array arguments.

Input
2

Values are stringified

Each array element is cast to string. Keys are skipped; order follows the array’s value order.

Transform
3

Separator inserted

Glue is placed between each pair of values—never after the last one.

Join
=

Combined string

Ready to echo, log, store, or pass downstream.

📝 Notes

  • join() is a strict alias of implode()—no behavioral difference.
  • PHP 8.0 requires separator-before-array argument order (legacy reversed order removed).
  • Empty array → ""; one element → that element alone.
  • Associative array keys are not included in the output.
  • For large arrays, join() is faster than concatenating in a manual loop.

Conclusion

The join() function is PHP’s readable name for joining array elements into a string. It is identical to implode()—use whichever name fits your codebase and team style.

Master separator-first syntax, pair with explode() when splitting and rejoining text, and pick one alias consistently across your project.

💡 Best Practices

✅ Do

  • Pick join() or implode() and stay consistent
  • Use join($separator, $array) order in PHP 8+
  • Prefer join() when code should read like English
  • Link to the implode() tutorial for deeper edge-case coverage
  • Use prepared statements instead of join for SQL value lists

❌ Don’t

  • Assume join and implode differ in behavior
  • Mix both names randomly in the same codebase
  • Use reversed PHP 7 argument order on PHP 8
  • Expect array keys in the joined string
  • Pass a string instead of an array (returns null with a warning)

Key Takeaways

Knowledge Unlocked

Five things to remember about join()

Use these points whenever you join arrays into strings in PHP.

5
Core concepts
🛠 02

Glue + Array

PHP 8 syntax.

Syntax
📝 03

Readable Name

Plain English.

Style
🔄 04

vs explode()

Join vs split.

Related
05

Stay Consistent

Pick one alias.

Team rule

❓ Frequently Asked Questions

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.
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+.
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.
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.
It returns an empty string, same as implode(). A single-element array returns that element with no separator added.
Conceptually yes: explode() splits a string into an array; join()/implode() merge an array into a string. For simple delimited text they are inverse operations.

Explore More PHP String Functions

Review the full string function reference or continue with PHP interview programs.

String Functions Overview →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful