Content · Programming · PHP

PHP explode() Function: Usage and Examples

Learn how PHP explode works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Splits a string at an exact delimiter into an array of pieces.

What Is explode() in PHP?

Splits a string at an exact delimiter into an array of pieces.

Its syntax is:

explode(string $separator, string $string, int $limit = PHP_INT_MAX): array

Parameters:

  • $separator — Accepted as string; required argument.
  • $string — Accepted as string; required argument.
  • $limit — Accepted as int; optional default PHP_INT_MAX.

Return type: array. An empty separator throws ValueError; explode is not a CSV parser because it does not understand quoted delimiters.

Availability: Core PHP.

Example 1: Basic Usage

<?php

print_r(explode(",", "red,green,blue"));

Output:

Array
(
    [0] => red
    [1] => green
    [2] => blue
)

An empty separator throws ValueError; explode is not a CSV parser because it does not understand quoted delimiters.

Example 2: A Different Input

<?php

print_r(explode("|", "one|two|three", 2));

Output:

Array
(
    [0] => one
    [1] => two|three
)

An empty separator throws ValueError; explode is not a CSV parser because it does not understand quoted delimiters.

Practical Example: Applying It in a Program

<?php

$row = "Alex|editor|active"; [$userName, $userRole, $accountStatus] = explode("|", $row, 3); echo $userName . " (" . $userRole . ")";

Output:

Alex (editor)

An empty separator throws ValueError; explode is not a CSV parser because it does not understand quoted delimiters.

Important Behavior and Edge Cases

  • Key behavior: An empty separator throws ValueError; explode is not a CSV parser because it does not understand quoted delimiters.
  • Environment: Core PHP.

Comparison With Related Functions

FunctionPurpose
explode()Splits a string at an exact delimiter into an array of pieces.
str_getcsv()str_getcsv() parses fields including quotes and escape rules; explode() splits strictly on a literal separator.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: An empty separator throws ValueError; explode is not a CSV parser because it does not understand quoted delimitersAn empty separator throws ValueError; explode is not a CSV parser because it does not understand quoted delimiters.

Key Takeaways

  • Splits a string at an exact delimiter into an array of pieces.
  • An empty separator throws ValueError; explode is not a CSV parser because it does not understand quoted delimiters.
  • Core PHP.

Summary: An empty separator throws ValueError; explode is not a CSV parser because it does not understand quoted delimiters.

Share this resourceSend this page to someone who may find it useful.