• Register
4,670 points
9 5 1

Arrays data can be converted to string in PHP by using Implode () function. Implode function joins the elements of an array to help to display array elements in string format. Another important function is “JOIN ()” which helps to join elements of an Array. If you have basic knowledge of how to use PHP functions and syntax then it's quite easy for you. Let’s see how Implode () function works.

Implode () how it works?

We will use two parameters in this function, let’s name the first parameter as the separator, and the other we will pass the array value. It will look like Implode (separator, array).

<?php
//Assign values to array name greatings 
$greatings=array(Hello,MRcoder,',', How, are, you);
//use implode funtion to convert array to string 
echo implode(" ",$greatings)."<br>";
?>

in the above example, we declared variable named greetings and passed values to it. Next, we used the implode () function and passed separator and $greetings to display string.

JOIN () function 

Join function is an alias of implode function it can also be used to convert array value to a string.lets see how it works.

<?php
//Assign values to array variable name arr
$arr=array('Hello','Mr','coder',',', 'How', 'are', 'you');
//use join funtion to convert array to string 
echo join(" ",$arr).".<br>";
echo join("+ ",$arr).".<br>";
echo join(" -",$arr).".<br>";
?>

if you found this post useful or for any queries comment please.

4,670 points
9 5 1