Home and Learn: PHP Programming Course

Joining text in PHP


In the previous PHP lesson, you'll learnt how to split a line of text. All the parts of the line of text will then be in an array.

But if you have a line of text in an array, you can join it all together to form a single line of text. This is just the opposite of explode. This time, use implode( ):

$seasons = array("Autumn", "Winter", "Spring", "Summer");
$new_textline = implode( ",", $seasons );

Here we have an array called $seasons. The text in the array needs to be joined before writing it back to a text file. The implode( ) function does the joining. The syntax for the implode( ) function is just the same as explode( ).

implode( separator, text_to_join )

So implode( ) will join all the text together and separate each part with a comma, in the code above. Of course, you don't have to use a comma. You could use a dash:

$new_textline = implode("-", $seasons)

Or any other character:

$new_textline = implode("#", $seasons)

Even a space:

$new_textline = implode( " ", $seasons)

The implode( ) function can come in handy, if you need to work with single lines of text.

In the next part, we'll take a look at something called Escaping.

<-- Back One Page | Move on to the Next Part -->

Back to the PHP Contents Page


Email us: enquiry at homeandlearn.co.uk