How to convert an array to a string with PHP

Converting an array to a string is a pretty frequent task when programming.
Whether you want to join some sentences into a paragraph or just smash the
values into some sort of hash, you can do so very simply with PHP’s implode
function.

Implode takes 1 to 2 arguments. In it’s simplest form, you can pass it your
array and the values will be joined by an empty string. The other option is to
pass in the “glue” as the first argument which is the string that will be used
to stick the array elements together. The glue is followed by the array:

$array = [
    'ABCDEFG',
    'HIJKLMN',
    'OPQRSTU',
    'VWXYZ',


echo implode($array) // ABCDEFGHIJ...

echo implode('::', $array // ABCDEFG::HIJ...
Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.