PHP stands for Hypertext Processor. The PHP programming language is based on the paradigm of Object-Oriented Programming (OOPs). It is an open-source language that is used for scripting and general-purpose programming, developed by Rasmus Lerdorf, a Danish-Canadian developer in 1994. 

PHP is one of the most popular programming languages and acts as the core of WordPress, which is one of the largest and finest platforms provided for blogging and creating websites. Its versatility and strength are the reason behind millions of programmers using PHP on a larger scale for web and application development. You can write programs that are enabled to generate high-level dynamic content that can be executed on the server by using PHP. PHP is quite flexible with usage, as it can be installed and used on almost all popular operating systems such as Windows, macOS, Linux, RISC OS, and many variants of UNIX as well. 

In this article, you are going to learn about implode in PHP. As you move further in the article, you will have a look at what is the use of implode in PHP, and how to use it in a program. You will explore some critical concepts of imploding in PHP, in-depth.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

What Is Implode in PHP? 

Implode in PHP is a function that is used to concatenate all the elements of an array together in the same order as they are in the array. And it, in turn, returns a new resultant string. This function is the same as the join() function in PHP, and both are an alias of each other. The implode() in PHP also works for the string having binary data passed as the parameter, making it a binary-safe function. You can provide the type of separation you want between the array elements in the resultant string. You need to provide the “separator string” which can be any string or character. If you don’t specify the type of separation to the implode(), then by default all your elements will be joined, with no space or any type of separation between them. 

You can also convert an array of zero, one, or more elements into a string using implode() in PHP. You have already seen arrays having over one element. However, an array with no element or just a single element can also be converted to a string using implode() in PHP. You need to pass the array and the separator. In this case, it doesn't matter, as you will get the array element as a string in the output.  You can also join the elements of an array as having key-value pairs in it.

Syntax and Parameters of Implode in PHP

To join the elements of an array using implode() in PHP, you can use the two ways described below.

Syntax of implode() in PHP:

  • Without passing the separator string to the implode()

// syntax when no separator is passed 

// to the implode()

implode ($array)

  • Passing a string as the separator string parameter

// syntax when a separator is passed 

// to the implode()

implode ($separator_string, $array)

Parameters and Their Description:

  • separator_string: This parameter is a string type parameter and it splits the array elements by inserting this string between each of the elements. You can provide any string or character to act as a separator for your resultant string. This is an optional parameter. If you don’t specify it, the implode() by default will insert an empty string in between the array elements. In other words, all the elements will be joined without any space or characters in between them.
  • array: This is the parameter that accepts an array as the argument and joins all the elements of this array using the provided separator, or the default separator, in case it is not passed. This array can have zero, one, or more elements in it. It can also be in many forms, such as arrays containing objects, arrays having key-value pairs or even the null values that are handled in implode() using the array_filter method in PHP.

The following example illustrates implode() in PHP with and without a separator string.

<?php

    // initialize an array

    // whose elements are to be

    // joined together.

    $arrayToBeJoined = array('Hi', 'There', '!');    

    // without passing the

    // separator string.

    echo 'Output string without the separator.';

    echo PHP_EOL;

    print_r(implode($arrayToBeJoined));    

    echo PHP_EOL;

    // "-" is passed as the first parameter

    // denoting the separator.

    echo 'Output string with the separator.';

    echo PHP_EOL;

    print_r(implode("-", $arrayToBeJoined));

?>

Implode_In_PHP_1.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

How to Separate the Array Elements With Different Characters?

As discussed earlier, the separator string can be any character or string. So, you can pass different characters to separate the elements of the array. You can use characters such as space (“ “), comma (,), hyphen (-), symbols like addition operator (+), multiplication operator (*), letters of the alphabet, or even digits and special symbols.

The following example illustrates how to separate the array elements with different characters.

<?php

    // initialize the array whose

    // elements are to be joined.

    $arrayToBeJoined = array('Example', 'Of', 'Implode', 'Function');

    // space character as the separator

    echo implode(" ", $arrayToBeJoined);

    echo "\n\n"; 

    // addition operator character as the separator

    echo implode("+", $arrayToBeJoined);

    echo "\n\n";

    // hyphen character as the separator

    echo implode("-", $arrayToBeJoined);

    echo "\n\n";

    // multiplication operator character as the separator

    echo implode("*", $arrayToBeJoined);

    echo "\n\n";

    // hash symbol as the separator

    echo implode("#", $arrayToBeJoined);

    echo "\n\n";

    // numeric character character as the separator

    echo implode("0", $arrayToBeJoined);

    echo "\n\n";

    // letter of alphabet as the separator

    echo implode("Z", $arrayToBeJoined);

    echo "\n\n";

?>

Implode_In_PHP_2

How to Join the Array Elements With a String?

Apart from using a character to separate the array elements, a string having over one character can also be used to do the same. You can provide any string having a length of more than one as the parameter.

The following example illustrates how to join array elements with a string.

<?php

    // initialize the array whose

    // elements are to be joined.

    // note that this a string having

    // binary elements in it.

    $arrayToBeJoined = array('1', '0', '0', '1', '1', '0');

    // initialize a string variable

    // to act as the separator.

    $separatorString = "HI";

    // string variable as the separator

    echo implode("$separatorString", $arrayToBeJoined);

    echo "\n\n";

    // string "xo" as the separator

    echo implode("xo", $arrayToBeJoined);

    echo "\n\n";

    // string of multiple characters as the separator

    echo implode("****", $arrayToBeJoined);

    echo "\n\n";

    // string of multiple letters as the separator

    echo implode("xyz", $arrayToBeJoined);

    echo "\n\n";

    // string having "\t" as the separator

    echo implode("\t", $arrayToBeJoined);

    echo "\n\n";

    // string having multiple spaces as the separator

    echo implode("   ", $arrayToBeJoined);

    echo "\n\n";

?>

Implode_In_PHP_3.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

How to Convert PHP Arrays to Strings?

Implode in PHP can serve multiple purposes, you can even convert an array to a string using this function. You just need to choose a suitable separator to separate the array elements. For example, if the array elements are forming a sentence, then you can simply use a space character. Or if the array elements are in the form of a list’s items, then you can use a comma (,) operator to separate them. The implode function can convert an array of any length i.e. 0, 1, or more than 1.

The following example illustrates how to convert PHP Arrays to Strings.

<?php

    // initialize the arrays whose

    // elements are to be joined.

    /****************************** 1. empty array *****************************/

    $array1 = array();

    /*************************** 2. array of length one ***********************/

    $array2 = array("A");

    /*********************** 3. array of length more than one *****************/

    $array3 = array('Hi', 'there');

    /******************** 4. array elements forming a sentence ****************/

    $array4 = array('I', 'am', 'a', 'sentence');

    /******************** 5. array having binary elements *********************/

    $array5 = array('1', '0', '0', '1', '1', '0');

    /******************** 6. array elements forming a list ********************/

    $array6 = array('Apple', 'Banana', 'Papaya');

    // the zero length array

    // will be converted to an empty

    // string. Note here, that 

    // the separator does not matter.

    echo implode("+", $array1);

    echo "\n\n";

    // with the one length array, 

    // the separator does not matter.

    echo implode(" ", $array2);

    echo "\n\n";

    // array of length

    // more than one.

    echo implode(" ", $array3);

    echo "\n\n";

    // choosing a space separator

    // for sentences like array elements.

    echo implode(" ", $array4);

    echo "\n\n";

    // choosing the default empty

    // string separator

    // for binary array elements.

    echo implode($array5);

    echo "\n\n";

    // choosing a comma separator

    // for lists like array elements.

    echo implode(", ", $array6);

    echo "\n\n";

?>

Implode_In_PHP_4.

Free Course: Programming Fundamentals

Learn the Basics of ProgrammingEnroll Now
Free Course: Programming Fundamentals

How to Convert an Array of Arrays to a String?

Another useful ability that ‘implode in PHP’ has is that it can be used to convert an array of arrays to an output string. In other words, an array having arrays as its objects can also be converted to a string. For example, there is an array called ShoppingList. This array can have multiple arrays as its objects, containing lists like fruits list, vegetable list, toys list, and so on. So using the implode function, you can easily convert this array of arrays into a single string.

The following example illustrates how to convert an Array of Arrays to a String.

<?php

    // initialize the array whose

    // elements are to be joined.

    // the array named "ShoppingList"

    // is an array having multiple arrays

    // as its objects.

    $ShoppingList = array(

        // array 1 defined as the object

        "List1" => array("Apple", "Mango", "Banana", "Orange"),

        // array 2 defined as the object

        "List2" => array("Bread", "Butter", "Cheese", "Oregano"),

        // array 3 defined as the object

        "List3" => array("Sugar", "Salt", "Walnuts", "Almonds")

    );

    /***** uncomment the below line to ****/

    /***** see the undesired output  ****/

    // echo implode(", ", $ShoppingList);  

    /**** output from above line: *****/

    /**** 'Array' 'Array' 'Array' ****/

    // Note: if you directly write the above 

    // line of code having implode(),

    // the output will not be the desired one you want.

    // function to rectify the above situation,

    // which avoids the output as Array Array Array

    // and prints the elements of the arrays.

    function implodeArrayofArrays($arr) {

        $resultString = '';

        foreach ($arr as $subarray) {

            $resultString .= implode(", ", $subarray);

        }

        return $resultString;

    }

    // print the resultant string 

    // of the array of arrays

    // using the implode function

    echo implodeArrayofArrays($ShoppingList);

    echo "\n\n";

?>

Implode_In_PHP_5 

Are you a web developer or interested in building a website? Enroll for the Full Stack Web Developer - MEAN Stack Master's Program. Explore the course preview!

Wrapping Up!

In this article, you have learned about Implode in PHP. You saw what implode in PHP is, and its syntax and parameters in depth. You explored important examples such as separating the array elements with different characters, joining array elements with a string, and so on. 

To get started with PHP, you can refer to this video.

Don’t just stop here. To learn MEAN stack development and to give yourself a chance to work for top tech giants, check out Simplilearn’s course on Full Stack Web Developer - MEAN Stack. In this course, you will learn some of the hottest skills like Node, Mongo, etc. that will help you set your foot in professional web development. 

If you have any questions for us, please mention them in the comments section and we will have our experts answer them for you!

Happy Learning!

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors