Implode in PHP is a vital function of PHP for one to learn if they aim to be a future developer or look for a career change into website development. The implode() function in PHP is a predefined, built-in, binary-safe function in PHP used for joining array elements with a string. Implode() is also known as PHP | join() function and it works similar to that of the join() function.
In this piece, we will further touch upon topics of Implode in PHP like parameters of Implode in PHP and its examples to deepen your knowledge about the topic.
So, to begin with, let’s first understand what exactly implode in PHP is.
Table of Contents
What is Implode in PHP?
The implode() function in PHP is called “array to a string,” as it takes an array of elements and returns a string. For example, if we want to join an array of elements to form one string, we can use the implode() function to perform the same. Similar to the join() function, implode() function in PHP will also return a string formed from all the components of an array utilizing a delimiter of our choice, which may be put in a variable.
What is PHP?
Before we go ahead to learn about the implode() function in PHP, we need to know what PHP is and what it is used for. PHP is a widely-used, general-purpose, open-source scripting language that is used for web development. Earlier, PHP stood for ‘Personal Home Page’; however, it is now called PHP: Hypertext Preprocessor. PHP runs on almost all platforms, including Windows, Linux, Unix, Mac OS X, among others.
It is also compatible with nearly all servers used today, including Apache, IIS, among others. Its ease of learning and efficient working on the server-side is one reason why it is used so widely. It is also free to download, and a user can get it from the official PHP resource site, which is www.php.net.
Implode in PHP: Syntax and parameters
Syntax (implode(separator, array)) is a set of rules, processes, or principles that govern the structure of sentences in a language, including the word order. And, in Implode in PHP, two syntaxes are used—here’s the ways function can be invoked in:
- The implode (string $glue, array $pieces): In this, the glue is used to combine the array components.
- The implode (array $pieces): Alternatively, in this, no glue is used—this is to make sure the pieces will be concatenated together.
The implode() function accepts two parameters. And, out of which, one parameter is optional, and the other is mandatory. These are the two parameters, and here’s their description:
The first parameter is ‘separator’
The separator is the optional parameter, and it is there to specify what is needed to be put between the array components. By default, it appears as ”, “ which denotes an empty string. The array values are joined to form a string and are separated by the separator parameter.
The second parameter is ‘array’
The array is the required parameter whose values are joined to form a string.
TIP: Although in implode(), the separator parameter is optional, it is highly recommended to use both the parameters always for backward compatibility.
Examples of Implode in PHP
Here’s an example of separating the array elements with different characters:
$arr = array(‘Hi’,’there!’,’How’,’are’,’you’);
echo implode(” “,$arr).”<br>”;
In the first expression, we have used a comma as a separator to obtain a string, whereas, in the second expression, we have used ‘space’ as a separator to demonstrate the implode function.
Hi there! How are you
Here’s an example of joining array elements with a string:
$arr = array(‘Have’,’a’,’nice’,’day!’);
echo implode(” “,$arr);
Have a nice day!
Example of converting PHP Arrays to Strings
$ar = [‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’];
echo implode(‘, ‘, $ar);
// Sunday, Monday, Tuesday, Wednesday
Example of converting an Array of Arrays to String
$School = [
‘Primary’ => [‘Class I’, ‘Class II’, ‘Class III’, ‘Class IV’],
‘Middle’ => [‘Class V’, ‘Class VI’, ‘Class VII’, ‘Class VIII’],
‘Secondary’ => [‘Class IX’, ‘Class X’, ‘Class XI’, ‘Class XII’]
echo implode(‘, ‘, $School);
// Array, Array, Array
// Class I, Class II, Class III, Class IV, Class V, Class VI, Class VII, Class VIII, Class IX, Class X, Class XI, Class XII
Learn about: PHP Developer Salary in India
What is the PHP explode function?
The opposite of PHP implode() function is PHP explode(). It is an in-built function in PHP that can split a string into various strings based on a string’s delimiter. The PHP implode() function splits the string wherever the delimiter character occurs, and it returns an array that contains the strings that exist after splitting the initial string.
Parameters in the PHP explode function
Unlike PHP Implode, the PHP explode function has three parameters. Out of which one is optional, whereas the other two are compulsory. Here is a brief description of the 3 parameters in PHP explode function:
The first parameter is the separator
A separator is a character that specifies a critical point or points where the string will divide. Whenever a separator is found in the code, it represents the end of the current element of the array and the beginning of the new element.
The second parameter is Original String
The Original String is the input string, which will be divided into arrays by this method.
The third parameter is No of Elements
The No of Elements parameter is an optional parameter and is used for detailing the number of characters of the array. The No of Elements parameter can be any integer, which means it can be positive, negative, or even zero.
// original string
$OriginalString = “Hi there, I hope you are well!”;
// Without optional parameter NoOfElements
// with positive NoOfElements
// with negative NoOfElements
 => Hi
 => there,
 => I
 => hope
 => you
 => are
 => well!
 => Hi there,
 => I
 => hope you are well!
 => Hi
 => there,
 => I
 => hope
 => you
PHP as a programming language evolved in 1994 and, since then, has been evolving very fast. As of September 2020, nearly two out of every three websites using PHP are still on old PHP versions—that have been discontinued, and almost half of all the PHP websites use version 5.6 or older.
India’s largest online higher education company, upGrad, brings you a range of courses for you to prepare for a better future in web development and software technology. Some of their top courses are PG Diploma in Software Development, Specialisation in Full Stack Development, and PG Diploma in Data Science.