What is implode function and how to use implode()

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.

The Implode() Function

The implode() is the string function in PHP which is used for converting an array into the string.

How To Use Implode() Function?

we can use implode function in PHP as given below.

Syntax

implode(separator,string)

Explanation

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

  • In the above code, we take an array into variable language then we use the implode() function for converting an array into the string. It stores the string into a variable named as str.
  • Here we have used “ ”( space ) as a separate of the string.
  • We use $language as a string parameter of the implode function.

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

  • First, we put an array into a variable message.
  • Then we use the implode function, and we pass space as a separator of string.
  • We store the resulted string into variable str.
  • And then we echo the string.

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

  • First, we put an array into a variable message.
  • Then we use the implode function, and we do not pass a separator of string.
  • We store the resulted string into variable str.
  • And then we echo the string.

Note

  • In implode() function separator parameter is optional, but if we do not use this parameter, then it shows the output without any separator.
  • For getting readable output, it is advisable to use a separator.

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

Conclusion

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.

 

Leave a Reply

Your email address will not be published.