implode()¶
The implode() function is used to join elements of an array into a glue, placed between each element.
implode() uses the first argument as the separator, and the second as the array. In older versions of PHP, this order could be reversed, but it recommended to avoid using it, as that feature was removed.
implode() has an alias called join().
implode() converts the elements of the array to string before merging them in a string. In particular, objects are converted to string with the magic-method, and sub-arrays are converted to Array, with a warning. Backed enumerations are not usable with that function.
implode() is faster to concatenate multiple strings than accumulating the concatenations in a single string. This happens because there is less need to allocate memory piecemeal.
<?php
declare(strict_types=1);
class x {
function __toString() {
return 'c';
}
}
$x = new x();
echo implode('a', ['a', 'b', new x()]);
?>
See also How to use implode() in PHP (with examples).
Related : Concatenation, Micro-optimisation, Separator