Soft blue gradient with a pale cyan glow in the upper left, calm and open mood

PHP Functions

The explode() Function in PHP

explode() splits a string into an array, breaking it apart at a delimiter you choose. This page covers its description, variables, return values, and examples with and without the limit parameter.

Back to the Learn PHP Tutorial home page

Description

explode() converts a string into an array of strings. You supply the delimiter that marks each break point, and the function returns the pieces in order.

The syntax is explode($delimiter, $string, $limit). The delimiter is required and cannot be an empty string; the string to split is required; the limit is optional.

Because the result is an array, you can loop over it, count its elements, or pass it to other array functions. It is a common way to read comma-separated values or to break a path into segments.

The explode function is one of the most practical tools you will encounter when learning PHP programming. It takes a string of text and splits it into separate pieces based on a delimiter you define, turning a single line of data into a manageable array. This is especially useful when working with form submissions, CSV data, or any scenario where information arrives as a single block but needs to be processed piece by piece. Understanding explode gives you greater control over how you handle and organize data within your applications. As you build your skills through video tutorials and hands-on practice, mastering functions like this will accelerate your progress from beginner to confident developer.

When you combine PHP functions like explode with a MySQL database, you unlock powerful ways to build dynamic web applications from scratch. Imagine collecting user input from a form, splitting it into structured parts, and then storing those pieces in a database for later retrieval. This kind of data manipulation is essential for creating reusable components and complete applications such as news boards or content management systems. The fastest way to learn PHP is by watching expert-led tutorials that walk through real examples step by step, allowing you to pause, rewind, and practice at your own pace. Spending focused time on these core concepts will build your programming skill far more efficiently than sifting through outdated documentation.

Linocut relief print in deep forest green and warm cream showing abstract interlocking geometric blocks that suggest a string being divided into separate pieces
One string, several pieces: the delimiter decides where the breaks fall.

Variables

Parameters
VariableMeaning
$delimiterThe boundary string that marks where the string is split. Required.
$stringThe input string to be divided into array elements. Required.
$limitOptional. Controls how many elements are returned and how the remainder is handled.

Return values

explode() returns an array of strings, each one a portion of the original string between delimiter occurrences.

  • Without a limit, every piece is returned.
  • With a positive limit, at most that many elements are returned, and the last element holds the rest of the string.
  • With a negative limit, that many elements are left off the end of the result.

Check the delimiter before calling the function: an empty delimiter is not a valid boundary for splitting.

Examples

Example without $limit

Every segment between commas becomes its own array element.

$list = "apple,banana,cherry";
$fruit = explode(",", $list);
print_r($fruit);

The array holds three elements: apple, banana, and cherry.

Example with $limit

A positive limit caps the number of elements and keeps the remainder together.

$list = "apple,banana,cherry";
$fruit = explode(",", $list, 2);
print_r($fruit);

The array holds two elements: apple, and banana,cherry as a single remaining piece.

Keep learning

Related material on this site for string handling, validation, and practical PHP projects.

Form Validation

Server-side checks that keep submitted data safe even when JavaScript is switched off.

Read the guide

Ajax Chat Application

Build a simple messenger interface with PHP and Ajax, from sending messages to real-time setup.

Open the tutorial

Community Forum

Ask questions and share what you have learned with other PHP learners.

Visit the forum