How to convert array to string PHP?


To convert array to string, use the concept of implode () in PHP. Let’s say the following is our array −

$sentence = array('My','Name','is','John');

To convert the above array to string −

,implode(" ",$sentence)

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
   $sentence = array('My','Name','is','John');
   echo "The string is=",implode(" ",$sentence);
?>
</body>
</html>

Output

The string is = My Name is John

Let us now see another PHP code wherein we will add separator as well −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
   $sentence = array('One','Two','Three');
   echo "The string is=",implode("*",$sentence);
?>
</body>
</html>

Output

The string is = One*Two*Three

Updated on: 12-Oct-2020

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements