Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconImplode() in PHP: PHP Implode() Function [With Syntax & Parameters]

Implode() in PHP: PHP Implode() Function [With Syntax & Parameters]

Last updated:
26th Oct, 2020
Views
Read Time
8 Mins
share image icon
In this article
Chevron in toc
View All
Implode() in PHP: PHP Implode() Function [With Syntax & Parameters]

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.

Check out our free courses to get an edge over the competition. 

So, to begin with, let’s first understand what exactly implode in PHP is. 

Ads of upGrad blog

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.

Explore our Popular Software Engineering Courses

Check out upGrad’s Advanced Certification in Blockchain

Read: Must Read 10 PHP Interview Questions and Answers

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.

Check out upGrad’s Advanced Certification in Cloud Computing

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.

Explore Our Software Development Free Courses

Implode in PHP: Syntax and parameters

Syntax 

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.

Parameters

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.

In-Demand Software Development Skills

Examples of Implode in PHP

Here’s an example of separating the array elements with different characters:

Input:

<!DOCTYPE html>

<html>

<body>

<?php

$arr = array(‘Hi’,’there!’,’How’,’are’,’you’);

echo implode(” “,$arr).”<br>”;

echo implode(“+”,$arr).”<br>”;

echo implode(“-“,$arr).”<br>”; 

echo implode(“X”,$arr);

?>

</body>

</html>

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.

Output:

Hi there! How are you

Hi+there!+How+are+you

Hi-there!-How-are-you

HiXthere!XHowXareXyou 

Here’s an example of joining array elements with a string:

Input:

<!DOCTYPE html>

<html>

<body>

<?php

$arr = array(‘Have’,’a’,’nice’,’day!’);

echo implode(” “,$arr);

?>

</body>

</html>

Output:

Have a nice day! 

Example of converting PHP Arrays to Strings

Input 

$ar = [‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’];

echo implode(‘, ‘, $ar);

Output 

// Sunday, Monday, Tuesday, Wednesday

Example of converting an Array of Arrays to String

Input 

$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

Output:

// 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

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

 

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. 

Example 

Input:

<?php

    // original string

    $OriginalString = “Hi there, I hope you are well!”;

    // Without optional parameter NoOfElements

    print_r(explode(” “,$OriginalString));

    // with positive NoOfElements

    print_r(explode(” “,$OriginalString,3));

    // with negative NoOfElements

    print_r(explode(” “,$OriginalString,-1));

?>

Output:

Array

(

    [0] => Hi

    [1] => there,

    [2] => I

    [3] => hope

    [4] => you 

    [5] => are

    [6] => well!

)

Array

(

    [0] => Hi there,

    [1] => I

    [2] => hope you are well!

)

Array

(

    [0] => Hi

    [1] => there,

    [2] => I

    [3] => hope 

    [4] => you 

)

Read our Popular Articles related to Software Development

Ads of upGrad blog

Checkout: 15 Exciting PHP Project Ideas & Topics For Beginners

Conclusion 

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.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1Is PHP better than JavaScript?

JavaScript is a computer language that is interpreted. It's a tool for making interactive websites. Server-side programming, mobile development, and gaming all use it. The accessibility of your website can be improved using JavaScript. to make pages that are lighter and load faster, PHP is a popular web development language for a variety of reasons. Its ease of use, flexibility and a vast range of features are only a few of the key reasons. PHP is also a reasonably simple language to pick up, making it a popular choice among newcomers. Furthermore, PHP is free and open-source, which implies that anyone can use it for free. Some think that PHP is superior because it is more powerful and diverse, while others feel that JavaScript is more straightforward to learn and use. In the end, the individual must choose which language they prefer.

2What are the benefits of learning the fundamentals of computer science?

The study of the theoretical and practical aspects of information and computation, as well as the design of computing systems, is known as computer science. It has a wide range of applications in sectors such as engineering, business, science, and medical. Learning to code can assist you in becoming a better problem solver. Coding might assist you in becoming more organised and efficient. You can become a more creative thinker by learning to code. Coding can aid in the development of strong problem-solving abilities. Coding might assist you in developing an analytical mindset.

3What are the roles of a frontend and backend developer?

The process of creating the user interface for a website or application is known as frontend development. Everything from the layout and font to the buttons and input fields falls under this category. These interfaces are created using HTML, CSS, and JavaScript by frontend developers. Backend development is the process of improving a website's or application's functionality. Back-end systems, databases, and APIs are all part of this process. Backend developers collaborate with front-end developers to build a fully functional website or application. If you want to work as a frontend developer, you should first learn to code, then design and build user interfaces. If you’re going to work as a backend developer, you should first learn how to code then how to create web apps and services.

Explore Free Courses

Suggested Blogs

8 Exciting Full Stack Coding Project Ideas &amp; Topics For Beginners
4449
A Full stack developer is an engineer who can design and develop an end-to-end application independently by handling all the work of coding, databases
Read More

by upGrad

19 Feb 2024

17 Interesting HTML Project Ideas &#038; Topics For Beginners [2024]
416086
Summary In this article, you will learn 10 Interesting HTML Project Topics. Take a glimpse below. A tribute page A survey form Technical documentati
Read More

by Rohan Vats

19 Feb 2024

How to Open json File in Excel
96701
What is JSON? JSON (JavaScript Object Notation) is a file format that is used for storing and exchanging data in the network. It is used to send data
Read More

by Rohan Vats

19 Feb 2024

JSP vs Servlet: Difference Between JSP &#038; Servlet [2024]
52691
Websites are collections of static files, for example, images, graphics, and HTML files. These websites are referred to as web applications if they pr
Read More

by Rohan Vats

19 Feb 2024

Java Developer Salary in India in 2024 [For Freshers &#038; Experienced]
900650
Wondering what is the range of Java developer salary in India? From choosing your first programming language to writing apps for Android and several
Read More

by Rohan Vats

19 Feb 2024

Polymorphism In OOPS: What is Polymorphism [Detailed Explanation]
124687
Polymorphism in OOPs is inseparable and an essential concept of every object-oriented programming language. An object or reference basically can take
Read More

by Rohan Vats

19 Feb 2024

Literals In Java: Types of Literals in Java [With Examples]
8248
Summary: In this article, you will learn about Literals in Java. Literals in Java Integral Literals Floating-Point Literals Char Literals String Lit
Read More

by Rohan Vats

19 Feb 2024

Top 30 Exception Handling Interview Questions and Answers [For Freshers &amp; Experienced]
35925
Exception handling is a concept that is implemented in algorithms to handle possible runtime errors, which may disrupt the normal flow of a program. S
Read More

by Rohan Vats

19 Feb 2024

Top 40 MySQL Interview Questions &#038; Answers For Beginners &#038; Experienced [2024]
130462
Have a Data engineering or data science interview coming up? Need to practice some of the most asked MySQL interview questions? The article compiles t
Read More

by Rohan Vats

19 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon