Member-only story
Implode and explode in PHP with the example -
Implode and explode in PHP are the two built-in functions of PHP. These are widely used in web applications. Sometimes, we want to insert a similar set of elements like selecting multiple dropdown values or inserting multiple checkbox values into the database in PHP.
With the help of implode functions, we can do this. In this tutorial, I will explain these two functions one by one.
Illustration of Implode and Explode in PHP
Implode function takes the input array elements one by one and holds the elements using a separator and returns a string by joining all the elements of the array with the separator.
Syntex:- implode(separator, array)
whereas
- “separator” separates the resultant string.
- and “array” is the input array that is taken as the multiple inputs.
Example:-
In this example, assume that we have a list of cars as an array that has to be shown or inserted in the database.
$cars = array(‘Maruti’, ‘Volvo’, ‘BMW’, ‘Marcedes’);
Now, if you want to echo the car’s array then you have to wrap the above array using the implode() function as shown below.
<?php $cars = array('Maruti','Volvo','BMW','Marcedes'); echo implode(' , ', $cars).'<br>'; echo implode(' + ', $cars).'<br>'; echo implode(' and ', $cars).'<br>'; echo implode(' or…