The PHP explode function splits a string into an array wherever a separator appears, for example a comma in "red,green,blue". It is one of the most used string functions in PHP, and it also comes up often in exams and interviews. This guide explains the syntax, the limit parameter, the special cases that changed in PHP 8 and the mistakes beginners make, and ends with a 25-question MCQ quiz. For more practice on strings, see our PHP strings MCQ post.
PHP explode Function Syntax
explode(string $separator, string $string, int $limit = PHP_INT_MAX): array- $separator: the text to split at. It can be one character like
","or several like", ". It cannot be empty. - $string: the string you want to split. It is not changed.
- $limit: optional. Controls how many elements you get back (explained below).
- Return value: an array of strings.
Basic Example
<?php
$topics = "quiz,lancer,coding,quizzes";
$parts = explode(",", $topics);
print_r($parts);
echo $parts[0]; // quiz
echo count($parts); // 4
/* Output of print_r:
Array
(
[0] => quiz
[1] => lancer
[2] => coding
[3] => quizzes
)
*/Every element is a string, even when it looks like a number. explode(",", "5,10") gives "5" and "10", so convert them with (int) or array_map('intval', ...) when you need numbers.
The limit Parameter
The third argument is the one most students forget. It changes the result in three different ways.
| limit | What happens | explode(“,”, “a,b,c,d”, limit) |
|---|---|---|
| Positive, e.g. 2 | At most that many elements. The last one holds the rest of the string. | [“a”, “b,c,d”] |
| Negative, e.g. -1 | Split everything, then drop that many elements from the end. | [“a”, “b”, “c”] |
| 0 | Treated as 1. | [“a,b,c,d”] |
| Not given | Split at every separator. | [“a”, “b”, “c”, “d”] |
<?php
$line = "name=Rahul=Patil";
// Split only at the first "=" so the value can contain "=" too
[$key, $value] = explode("=", $line, 2);
echo $value; // Rahul=Patil
$path = "uploads/2026/11/photo.jpg";
print_r(explode("/", $path, -1)); // uploads, 2026, 11 (file name dropped)
Special Cases and PHP 8 Changes
- Separator not found:
explode(",", "hello")returns["hello"], an array with the whole string as its only element. - Empty separator:
explode("", "PHP")throwsValueError: explode(): Argument #1 ($separator) cannot be emptyin PHP 8. Older PHP 7 code returnedfalsewith a warning. To split into single characters, usestr_split(). - Empty string:
explode(",", "")returns[""], one empty element, not an empty array. Socount()is 1. - null as the string:
explode(",", null)also returns[""], and since PHP 8.1 it shows a deprecation notice. Check the value before you call explode(). - Separator at the start, end or twice in a row: you get empty strings.
explode(",", ",a,,b,")gives["", "a", "", "b", ""]. - Case-sensitive:
explode("A", "bAnana")only cuts at the capital A and gives["b", "nana"].
Extra Spaces and Several Separators
explode() cuts at the exact separator and keeps everything else, including spaces. These are the three fixes you will need most often.
<?php
// 1. Space after each comma: trim every element
$colors = array_map('trim', explode(",", "red, green , blue"));
// ["red", "green", "blue"]
// 2. One or more spaces, tabs or new lines between words
$words = preg_split('/\s+/', trim("PHP is \t fun"));
// ["PHP", "is", "fun"]
// 3. Different separators at once (comma or semicolon)
$items = preg_split('/[,;]/', "pen,book;bag");
// ["pen", "book", "bag"]Old tutorials also mention split(). That function was removed in PHP 7, so use explode() for a fixed separator and preg_split() for a pattern.
explode() vs implode()
| Feature | explode() | implode() |
|---|---|---|
| Works on | A string | An array |
| Returns | An array | A string |
| Argument order | explode(separator, string) | implode(separator, array) |
| Typical use | Read a CSV line, tags, a date like 2026-11-19 | Build a CSV line or a comma list for display |
<?php
$tags = explode(",", "php,html,css"); // ["php", "html", "css"]
echo implode(" | ", $tags); // php | html | css
// Read parts straight into variables
[$user, $domain] = explode("@", "quiz@lancer.com");
echo $domain; // lancer.com
list($year, $month, $day) = explode("-", "2026-11-19");
echo $month; // 11You can read every detail of the function in the official PHP manual for explode().
PHP explode Function MCQ Quiz
Test yourself now. The quiz has a pool of 25 questions and every attempt gives you 10 random ones. Press Submit at the end to see your score, the correct answers and a short explanation for each question.
Frequently Asked Questions
Can explode() use more than one separator?
No. explode() takes one separator string. To split at a comma or a semicolon, use preg_split('/[,;]/', $str).
What is the difference between explode() and str_split()?
explode() cuts the string wherever the separator appears. str_split() has no separator and cuts the string into pieces of a fixed length, one character each by default.
Why do I get empty elements after explode()?
Because the separator appears at the start or end of the string, or twice in a row, for example two spaces. Use preg_split('/\s+/', trim($str)) for words, or remove empty items with array_filter().
Conclusion
The PHP explode function turns a string into an array in one line. Remember the argument order (separator first), use limit when only the first few pieces matter, watch for extra spaces and empty elements, and switch to preg_split() when you need a pattern. In PHP 8 an empty separator throws a ValueError, so check your input first. Try the examples in a test file, then take the quiz above and read the explanation for every answer you got wrong.
Get one new quiz every week. Join Quiz Lancer on Telegram →
Full-stack developer with 5+ years of experience in PHP, Laravel and JavaScript. I create these quizzes to help students and freshers prepare for web development interviews.



