In this article, you will learn about how to convert the array into a string in PHP. The implode() function converts the array into a string or in other words, it returns a string by joining the elements of the array.
Implode() function is an alias of the join() function. Also, it accepts the parameters in both lefts to right/right to left order. However, they are passed in the order of convention (like explode() function).
You can also specify the separator character but it is not mandatory. However, for backward compatibility, you must use the two arguments.
This function is binary-safe. Binary safe functions are those, that produce the same output even if there are some special characters present in the string. Their output is not disturbed by the special chars.
WHAT IS THE SYNTAX OF THE JOIN() FUNCTION IN PHP?
implode(separator,array)
Parameters | Details |
---|---|
separator | It specifies the character or string to place between two array elements while joining them – Optional – Default value is empty string or “” |
array | The array to consider to convert to string |
EXAMPLES OF THE implode() FUNCTION
Example 1. In this example, we take an array with some string elements in it. We created a string by joining all the strings of the array.
<?php
$arr = array('Hello','PHP!','Sunny','Weather!');
echo implode(" ",$arr);
?>
Example 2. In this example, we join the array by using different separator characters.
<?php
$arr = array('Hello','PHP!','Sunny','Day!');
echo implode(/ ",$arr);
echo implode("AB",$arr)";
echo implode("-",$arr);
echo implode("_",$arr);
?>