implode function in php, php implode, php explode,  explode function in php, Implode and Explode Functions in PHP, how to use implode and explode in php

Implode and Explode Functions in PHP

By nature, PHP provides functions that work to ensure a certain activity or goal is achieved. This makes it simple to work with PHP since you do not need to redefine the function functionality as it is already done in PHP. Functions enable the developer to quickly and easily solve a problem with ease.

In this article, we shall discuss the implode and explode functions in PHP by defining what they do, where they are used, and what is required to use them.

Implode function

The implode function in PHP is commonly known as an array to string since it collects an array as input data and returns a string as the output data. The implode function is used to join elements in an array with a string.

The syntax of implode function in PHP is as below

implode(separator,array)

separator is an optional parameter and is set to empty by default

array is a required parameter which is the array that will be joined to string

Example of implode function

We first define an array for example

array('this','is','an','implode', 'function')

We can convert this array to a string by adding a space inside the words as below

<?php

$array=array('this','is','an','implode', 'function');

echo  implode(" ",$array);

?>

To explain the above,

The " " is the separator that will be included after each array element.

The $array is the array data that will be converted to a string.

The result of the above example will be

this is an implode function

You can add any separator to your array like the ones shown in the examples below

<?php
//Example 1 query
$array=array('this','is','an','implode', 'function');
echo implode("-",$array);
//Example 1 result
this-is-an-implode-function
//Example 2 query
$array=array('this','is','an','implode', 'function');
echo implode("/",$array);
//Example 2 result
this/is/an/implode/function
//Example 3 query
$array=array('this','is','an','implode', 'function');
echo implode(",",$array);
//Example 3 result
this,is,an,implode,function
?>

 

Explode function

The explode function in PHP does the opposite of implode function where it is used to "Split a string by a specified string into pieces i.e. it breaks a string into an array". Where the implode function forms a string as a result, the explode function takes a string as input and forms an array as the result.

The explode function is used to break a large string into a simpler smaller sizes of text depending on the separator you define.

The syntax for explode function in PHP is

explode (separator, string)

The separator is what separates the string.

For example, for implode example above we added a (-) sign as the separator for the string. In the explode function we have to define it as the separator if we want to obtain back the array we started with.

$string="this-is-an-implode-function";
echo explode ("-",$string);

The result for the above code snippet will be an array

Array
To display the values in this array we can use json_encode function to convert the array into an object so that it can be visible.

 

$string="this-is-an-implode-function";
echo json_encode(explode ("-",$string));

The result will now be as follows

["this","is","an","implode","function"]
Please note that you cannot explode a separator that is not present in a string.
In the above article, we have explained what implode function is in PHP, explode function is in PHP. We have also shown the syntax of each function and some examples using each function.