PHP explode() Function: Syntax, Examples and 25 MCQs

PHP explode function - Quiz Lancer

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.

limitWhat happensexplode(“,”, “a,b,c,d”, limit)
Positive, e.g. 2At most that many elements. The last one holds the rest of the string.[“a”, “b,c,d”]
Negative, e.g. -1Split everything, then drop that many elements from the end.[“a”, “b”, “c”]
0Treated as 1.[“a,b,c,d”]
Not givenSplit at every separator.[“a”, “b”, “c”, “d”]
How the limit parameter changes the result of explode()
<?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)
PHP explode function cheat sheet: limit 2, -1, 0 and empty separator
PHP explode function cheat sheet: limit 2, -1, 0 and empty separator

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") throws ValueError: explode(): Argument #1 ($separator) cannot be empty in PHP 8. Older PHP 7 code returned false with a warning. To split into single characters, use str_split().
  • Empty string: explode(",", "") returns [""], one empty element, not an empty array. So count() 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()

Featureexplode()implode()
Works onA stringAn array
ReturnsAn arrayA string
Argument orderexplode(separator, string)implode(separator, array)
Typical useRead a CSV line, tags, a date like 2026-11-19Build a CSV line or a comma list for display
Difference between explode() and implode() in PHP
<?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;   // 11

You 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.

PHP Explode Function MCQ Quiz

1 / 10

What does explode("A", "bAnana") return?

2 / 10

$parts = explode(",", "red, green"); What is $parts[1]?

3 / 10

What is the type of the elements in the array returned by explode(",", "5,10")?

4 / 10

What does explode(",", "a,b,c,d", 2) return?

5 / 10

After $parts = explode(",", $csv); what happens to $csv?

 

6 / 10

What happens when you run explode("", "PHP function") in PHP 8?

 

7 / 10

Which code splits a sentence into words when there can be one or more spaces, tabs or new lines between them?

8 / 10

What does explode(",", "hello") return?

9 / 10

What does the explode() function do in PHP?

10 / 10

What is the output of this code?

list($user, $domain) = explode("@", "quiz@lancer.com");
echo $domain;

Your score is

The average score is 0%

0%

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 →

Scroll to Top