Implode And Explode In PHP
Last Updated : Jan 1, 2023
IN - PHP
In this tutorial we will show you the solution of implode and explode in PHP, in our series of PHP tutorials, many times we come in touch with both implode and explode functions.
But in this tutorial, we are going to understand implode and explode functions.
Let us discuss both of them and also see the difference between them.
Step By Step Guide On Implode And Explode In PHP :-
Now, before going to discuss implode and explode let us see the syntax of both of them.
Here, below we are going to show you how to use both of them in codes. So, that you can better understand them.
<!DOCTYPE html> <html> <head> <title> implode and explode in PHP </title> </head> <body> <h1> TalkersCode </h1> <h2> Implode in PHP </h2> <?php $string = array('Welcome','to','Talkers','Code','World'); // conversion of array to string using space echo implode(" ",$string)."<br>"; // conversion of array to string using _ sign echo implode("#",$string)."<br>"; // conversion of array to string using - sign echo implode("-",$string)."<br>"; // conversion of array to string using X sign echo implode("X",$ string); // conversion of array to string using + sign echo implode("+",$ string); ?> <br> <h2> Explode in PHP </h2> <?php <?php $str = 'Welcome,to,Talkers,Code,World'; print_r(explode(',',$str,0)); print "<br>"; print_r(explode(',',$str,5)); print "<br>"; print_r(explode(',',$str,-2));?> </body> </html>
- As, here we see that we that in above example we show you an example in which HTML and PHP codes are used.
- Here, first of all, we create a basic structure of HTML, in which we use <!DOCTYPE html> which defines the type of document. And next one is our HTML tags. These tags are paired tags and all the data regarding HTML is written inside these tags.
- After we use our head tag which is again paired tag and contains the title and meta information of the webpage. The data written inside the head is not shown on the webpage.
- Now, next is the body which is the main tag of HTML. The data which we have written inside the body is shown on the webpage. Mostly all tags which are helpful to show data or information on the screen are written under the body tag.
- Now, as we above see that we give two examples, the first example is of implode function and another one is of explode function. Now, let us understand them in one line.
- Implode function is used to convert array to string whereas Explode is used to convert string to array. Syntex of both is the same, but the input and outputs must be different. For better understanding, you can run the above example to see the output which they give.
- In the case of implode function, we give various examples of converting the array to string whereas, in the case of explode, we use explode function in various ways to see the different outputs.
At last, in conclusion, here we can say that with the help of this article we can understand implode and explode function in PHP.
I hope this tutorial on implode and explode in PHP helps you and the steps and method mentioned above are easy to follow and implement.