Sometimes we require to convert an array into the string. PHP provides the feature to convert an array into the string. We can use implode() function to convert an array into the string.
Index
The implode() is the string function in PHP which is used for converting an array into the string.
we can use implode function in PHP as given below.
implode(separator,string)
1) separator
It indicates where you want to break the string.
It is the optional property of the implode function.
2) string
It indicates the string which you want to separate.
It is the required property of explode function.
Example 1
$language = array('HTML','CSS','JAVASCRIPT','PHP','PYTHON','JQUERY');
$str = implode(“ ”,$language); Explanation
Note: We can use different symbols as a separator. Example: space, comma, dot, high fun, and many more.
Example 2
Suppose we have an array as follows.
Array
(
[0] => Hello,
[1] => I,
[2] => am,
[3] => errorsea,
[4] => best,
[5] => teaching,
[6] => platform,
[7] => for,
[8] => students
)
<?php
$message = array('Hello','I','am','errorsea','best','teaching','platform','for','students');
$str = implode(" ",$message);
echo $str;
?> Output
Hello I am errorsea best teaching platform for students
Explanation
Example 3
Suppose if we use the implode function without using the separator parameter, then it shows the output as follows.
<?php
$message = array('Hello','I','am','errorsea','best','teaching','platform','for','students');
$str = implode($message);
echo $str;
?> Output
HelloIamerrorseabestteachingplatformforstudents
Explanation
Note
Example 4
If we use the hyphen ( – ) as a separator, then we will get the output as below.
<?php
$message = array('Hello','I','am','errorsea','best','teaching','platform','for','students');
$str = implode("-", $message);
echo $str;
?>
Output
Hello-I-am-errorsea-best-teaching-platform-for-students
Explanation: Here hyphen is a separator of string.
Example 5
If we use a full stop ( . ) as a separator, then we will get the output as below.
<?php
$message = array('Hello','I','am','errorsea','best','teaching','platform','for','students');
$str = implode(".",$message);
echo $str;
?> Output
Hello.I.am.errorsea.best.teaching.platform.for.students
Explanation: Here the full stop is a separator of string.
Example 6
If we use comma ( , ) as a separator, then we will get the output as below.
<?php
$message = array('Hello','I','am','errorsea','best','teaching','platform','for','students');
$str = implode(",",$message);
echo $str;
?> Output
Hello,I,am,errorsea,best,teaching,platform,for,students
Explanation: Here comma is a separator of string.
Also Read: PHP explode() Function
These are the different forms of implode() function in PHP. We can use them according to the requirement of the program. It is beneficial when we want to get a string from elements of an array.
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…