How to convert an array to a string with PHP

Josh Sherman
1 min read
Software Development 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...
Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

Related Articles