Home >>PHP String Functions >PHP implode() Function

PHP implode() Function

PHP implode() Function

PHP implode() function is used to create a string by joining the elements of the given input array. Implode() function is an in-built function of PHP. PHP implode() function works exactly the same as the PHP join() function. Just like the join() function in PHP implode() function also we pass an array as the input and the function returns an string by joining the elements of the given input array. Implode() function allows two parameter out of which only one is required & the other one is optional.

Syntax:

 implode($separator,$array);

Parameter Values

Parameter Description
separator This is an optional parameter. This parameter specifies what is to be put between the elements of the array. By default it is "" (an empty string).
array This is a required parameter. This parameter contains the array which we have to join to a string.

Here is an example of implode() function in PHP:

<html>
<body>

<?php
$a = array('This ','is ','a ','simple ','example ','of ','implode() ','function.');
echo implode($a);
?>

</body>
</html>
Output:
This is a simple example of implode() function.

Here is an another example of implode() function in PHP with different separator value:

<html>
<body>

<?php
$a = array('This','is','an','example','of','implode()','function','with','seperators');
echo implode(" ",$a)."<br>";
echo implode("_",$a)."<br>";
echo implode("--",$a)."<br>"; 
echo implode("::",$a);
?>

</body>
</html>
Output:
This is an example of implode() function with seperators
This_is_an_example_of_implode()_function_with_seperators
This--is--an--example--of--implode()--function--with—seperators
This::is::an::example::of::implode()::function::with::seperators

Popular Tutorials