How to convert array to string PHP?

PHPServer Side ProgrammingProgramming

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
raja
Published on 12-Oct-2020 15:43:03
Advertisements