Computer >> Computer tutorials >  >> Programming >> PHP

What is implode() function in PHP?


In this article, learn how to use PHP Implode() function.The Implode() function is a predefined inbuilt PHP function. We can form a string by joining the components of an array with the help of the PHP implode function. The implode() function creates a string from components of an array utilizing a delimiter of our choice. The implode() function acknowledges two parameters out of which one is optional and one is mandatory. 

Let's learn those parameters.

Syntax

implode(separator,array)

Parameters

separator

This is an optional parameter and is of string type. The values of the array will be join to form a string and will be separated by the separator parameter provided here.

array

The array whose value is to be joined to form a string.

Example

<?php
   $arr = array ('Welcome','To','Tutorials','Point');
   $comma_separated = implode(" , ", $arr);
   $hyphen_separated = implode(" - ", $arr);
   echo $comma_separated"\n" ;
   echo implode(" ",$arr)"\n";
   echo $hyphen_separated;
?>

Output

Welcome , To , Tutorials , Point
Welcome To Tutorials Point
Welcome - To - Tutorials - Point

Explanation

In the first expression, we have used a comma "," as a separator to form string. In the second expression, we have used space " " as a separator to demonstrate the implode function.