A couple of years ago we published a short article with some tips on good string functions. Didn’t think it would be that appreciated, but lots of people has emailed us and thanked us for the examples and ideas on how to use the functions. So here we go again, a new function to explain: implode a great function that helps you do simple things in a shorter way.

implode

The official PHP site summarizes the implode function like this: Join array elements with a string.

We’ll try to explain the function a little bit easier, even though it’s not a complicated function it can always help to get some examples and an easy explanation. At first you might think that you never need this function, but you are wrong. It can save you from creating for/while loops to concatenate the values from an array into a string. Let’s say you have a html form where the user can enter a lot of data and you want to display the data in a comma separated list for the user. You can always do a for loop like this:

<?php
for ($i= 0; $i < sizeof($_POST); $i++)
{
   $string .= $_POST[$i] . ",";
}
echo "You entered this in the form: " . $string;
?>

This is 5 rows for a really simple thing…
Let’s try another way using the function implode:

<?php
$string = implode(",", $_POST);
echo "You entered this in the form: " . $string;
?>

implode takes two parameters and the second one is the array with all the values that should be glued together as a string. The first parameter is what you want to put in between the values. The advantage with example two is (besides the shorter version) is that you don’t get a colon after the last value. Often you don’t want the last colon, so implode helps you with that in a simple way.

implode

The official PHP site summarizes the implode function like this: Join array elements with a string.

We’ll try to explain the function a little bit easier, even though it’s not a complicated function it can always help to get some examples and an easy explanation. At first you might think that you never need this function, but you are wrong. It can save you from creating for/while loops to concatenate the values from an array into a string. Let’s say you have a html form where the user can enter a lot of data and you want to display the data in a comma separated list for the user. You can always do a for loop like this:

<?php
for ($i= 0; $i < sizeof($_POST); $i++)
{
   $string .= $_POST[$i] . ",";
}
echo "You entered this in the form: " . $string;
?>

This is 5 rows for a really simple thing…
Let’s try another way using the function implode:

<?php
$string = implode(",", $_POST);
echo "You entered this in the form: " . $string;
?>

implode takes two parameters and the second one is the array with all the values that should be glued together as a string. The first parameter is what you want to put in between the values. The advantage with example two is (besides the shorter version) is that you don’t get a colon after the last value. Often you don’t want the last colon, so implode helps you with that in a simple way.

Similar Posts:

  • Share/Bookmark