How to Convert Array to String in PHP with Examples

Last Updated on May 29, 2021 by Roshan Parihar

In this tutorial, learn how to convert array to string in PHP with examples. The short answer is to use the implode() for conversion in PHP.

You can also use the join() function of PHP that works the same as the implode(). The for loop with concatenation (.) operator can also give you the required result.

So, let’s start converting an array to a string with the examples given here.

How to Convert Array to String in PHP with Implode()

To convert the array to string, you can use the implode() function. It takes two arguments which the first argument is the separation between the array elements after conversion. The second argument is the array variable that you want to convert to string.

Output

Welcome to Tutorialdeep!

The above example contains the array variable with three elements. After conversion, the resulted string contains three words that are the elements of an array. The separation is the space between the array element that you have defined in the function. You can change the separation type by changing the first argument of the function.

Change Array to String in PHP with Join()

You can change the array to string using the join() that works the same as the implode() function. The join() takes two arguments that can be the same as the previous example.

Output

Learn PHP with Tutorialdeep.

The above example gives a similar result as you have obtained using the implode() function. You can also change the separation type by changing the second argument of the function.

Using For Loop and Concatenation (.) Operator in PHP

In addition to above all methods, you can also use the for loop with concatenation (.) operator in PHP. Initially, you have to declare an array and an empty string to add an array element after conversion. The concatenation operator adds the array elements to the string on each iteration of the loop. Extra space is added between the array elements on conversion inside the for loop.

Output

Coding in PHP is amazing.

The output shows the added array elements using the for loop and concatenation operator in PHP.

Out of the above all methods, the implode() function and the join() function gives the faster result. The for loop takes some time to give the required output.

You May Also Like to Read

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.