PHP Implode Function (PHP Array to String)

The PHP implode function does the opposite of the PHP explode function. That is, it takes an array of strings and joins them together into one string using a delimiter (string to be used between the pieces) of your choice.

For example, if you had an array of words, you could use the PHP implode function to join them together using the space character as the delimiter and effectively make a sentence.

The implode function in PHP is easily remembered as "array to string", which simply means that it takes an array and returns a string. Have a look at the PHP implode function tutorial below.

PHP Implode Syntax

Below is the syntax of the implode function in PHP:

$result_string = implode($glue, $pieces);

Where $glue is the string that should be placed between each string in the $pieces array.

PHP Implode Example

Here is an example of the PHP Implode function in use:

<?php
 
// define our array of words
$words = array('I', 'love', 'Tutorial', 'Arena');
 
// output the words that will make up the sentence
for ($i = 0; $i < count($words); $i++)
{
    echo 'Word ' . $i . ' - ' . $words[$i] . '<br />';
}
 
// make and output a sentence using a space as the delimiter
$sentence = implode(' ', $words);
echo $sentence;
 
echo '<br />';
 
// make and output a sentence using a plus sign as the delimiter
$crazy_sentence = implode('+', $words);
echo $crazy_sentence;
 
?>

And the output would be:

Word 0 - I
Word 1 - love
Word 2 - Tutorial
Word 3 - Arena
I love Tutorial Arena
I+love+Tutorial+Arena

As we can see above, the various words from our array were joined together first using a simple space and then using the plus sign. The delimiters used need not be single characters. You can even use entire words to separate the strings when they are being joined if necessary. Go ahead and try it for yourself using the code from the above tutorial as a guide.

You may also want to have a look at the PHP Explode Function which does the opposite of this PHP Implode Function.

That's it for this tutorial on the PHP implode function.

Link to this Page

Thank Tutorial Arena for This Tutorial.
Show your appreciation with a +1...