PHP Function explode

The PHP explode function is one of the most used PHP functions, at least by me. It is used to turn a string into an array. Let’s say you had the following string:

$str = “Pizza, Pork, Ham, Sub, Chicken, Lamb, Rice, Noodles”;

You can very easily turn such a string into an array using the PHP explode function like this:

$foods = explode(“,”, $str);

Now you have the equivalent of this:

$foods = array(‘Pizza’, ‘Pork’, ‘Ham’, ‘Sub’, ‘Chicken’, ‘Lamb’, ‘Rice’, ‘Noodles’);

Here is a complete PHP example that demonstrates the use of explode:

$str = “Pizza, Pork, Ham, Sub, Chicken, Lamb, Rice, Noodles”;

$foods = explode(“,”, $str);

foreach($foods as $food){

echo $food.”<br />”;


The above example simply uses explode to break the $str string into the $foods array by splitting the string up by the commas.


The PHP explode function takes two arguments:

explode(‘split-by’, $string);

  • split-by – can be any value in $string that you want to separate the string into an array by.
  • $string – is the string you want to turn into an array.


This post should give you a good idea of how to use the explode function. If not, feel free to post your comments on this post and I’ll be happy to explain. Also, feel free to publish your own example code that users explode in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *