🚨 $100K in Sight: Follow Bitcoin’s Final Push Live! TRACK NOW

Code has been added to clipboard!

PHP explode()

Reading time 1 min
Published Aug 14, 2017
Updated Oct 10, 2019

PHP explode: Main Tips

  • PHP explode function is used to get an array of strings from a single string.
  • Using explode() in PHP is completely binary-safe.
  • It was introduced in PHP4 with the limit parameter, though negative limits weren't allowed until PHP 5.1.
DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
edX
Pros
  • A wide range of learning programs
  • University-level courses
  • Easy to navigate
  • Verified certificates
  • Free learning track available
Main Features
  • University-level courses
  • Suitable for enterprises
  • Verified certificates of completion

Syntax: Parameters Explained

The correct PHP explode function syntax is displayed below:

explode(separator,string,limit);

Let's figure out what each parameter represents when using PHP explode function:

Parameter Description
separator Needed. Defines the place where should PHP explode string. Cannot be an empty string!
string Needed. Specifies the exact string to split.
limit Optional. Defines the number of elements in an array to return after:
> 0 - Returns the array including a max of limit element(s)
< 0 - Returns the array except for the last -limit elements()
0 - Returns an array with one element

Usage: Code Examples

Look at the example below. It shows different outcomes that can be achieved using limit parameter with PHP explode function:

Example
<?php
  $string = 'first,second,third,fourth';

  // limit - zero
  print_r(explode(',', $string, 0));

  // limit - positive
  print_r(explode(',', $string, 2));

  // limit  - negative
  print_r(explode(',', $string, -1));
?>

In this next example, comma is used as a separator. See how that turns out:

Example
<?php
  $string = "This is example, with, commas!";
  print_r (explode(",", $string));
?>