PHP 8 reference · String Functions
PHP implode() Function
Join array elements into one string with a separator between them. join() is an alias of implode().
PHP 8 Since 4.0
Syntax
implode(string $separator, array $array): string
Parameters
| Parameter | Description |
|---|---|
separator optional |
The text placed between elements. Default: "" |
array |
The values to join. |
Return value
A single string.
Examples
<?php
$tags = ["php", "arrays", "strings"];
echo implode(", ", $tags), "\n";
echo implode($tags), "\n";
Output
php, arrays, strings phparraysstrings
<?php
$params = ["page" => 2, "sort" => "name"];
$pairs = [];
foreach ($params as $k => $v) {
$pairs[] = "$k=$v";
}
echo implode("&", $pairs), "\n";
Output
page=2&sort=name
Version history
In other languages
str.join()· Pythonarray.join()· JavaScript
Related
explode()· Split a string into an array by a separator, like turning "a,b,c" into ["a", "b", "c"]. A limit caps the pieces.array_map()· Apply a callback to every element of one or more arrays and return a new array of the results.http_build_query()· Build a URL-encoded query string, such as page=2&sort=name, from an array. Nested arrays become key[]=... pairs.- PHP Strings · tutorial