Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

implode

Joins array elements into a string.

Available since: PHP 4

Syntax

implode(string $separator, array $array): string

Parameters

ParameterTypeDescription
$separatorstringThe string placed between the elements.
$arrayarrayThe array whose elements are joined.

Return value

stringA string of the array elements separated by `$separator`.

Examples

A comma-separated list:

<?php
 
echo implode(', ', ['red', 'green', 'blue']);

Output:

red, green, blue

Joining without a separator, and with numbers:

<?php
 
echo implode('', ['P', 'H', 'P']), "\n";
echo implode(' + ', [1, 2, 3]);

Output:

PHP
1 + 2 + 3

Notes

Numbers and other scalar values are converted to strings automatically. explode() does the reverse.

See also

  • explode — Splits a string into an array by a separator.

Related recipes

In the tutorial

  • Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.

Thanks for reading!

Donate to the site — we work hard for you.