Introduction
PHP provides two useful functions for converting between strings and arrays: explode() and implode(). The explode() function splits a string into an array using a separator, while implode() joins array elements into a single string. These functions are commonly used when processing CSV-like data, employee records, product lists, and form inputs. In this chapter, you will learn both functions PHP Exploding and Imploding Strings Practice through practical PHP examples.
Question 1: Split a String Into an Array Using explode()
Problem Statement
Create a PHP program that splits the string "PHP,Java,Python,SQL" into an array using a comma as the separator.
PHP Solution
<?php
$languages = "PHP,Java,Python,SQL";
$result = explode(",", $languages);
print_r($result);
?>
Sample Output
Array
(
[0] => PHP
[1] => Java
[2] => Python
[3] => SQL
)
Explanation
The explode() function splits a string wherever it finds the specified separator. Here, the comma separates the four programming languages into individual array elements.
Array indexes start at 0.
Concepts Covered
explode()- String splitting
- Array indexes
Question 2: Split a Full Name Into Separate Words
Problem Statement
Split the full name "Rahul Kumar Sharma" into an array of words using a space as the separator.
PHP Solution
<?php
$fullName = "Rahul Kumar Sharma";
$nameParts = explode(" ", $fullName);
print_r($nameParts);
?>
Sample Output
Array
(
[0] => Rahul
[1] => Kumar
[2] => Sharma
)
Explanation
The space character is used as the separator. PHP divides the full name at each space and stores each word in a separate array element.
This can be useful when processing names or separating words from a text field.
Concepts Covered
- Splitting text by spaces
explode()- Accessing words through array indexes
Question 3: Extract the First Name From a Full Name
Problem Statement
A company stores an employee’s full name in one string. Extract and display only the first name from "Amit Sharma".
PHP Solution
<?php
$fullName = "Amit Sharma";
$nameParts = explode(" ", $fullName);
echo "First Name: " . $nameParts[0];
?>
Sample Output
First Name: Amit
Explanation
The explode() function separates the full name into an array. The first name is stored at index 0, so $nameParts[0] returns "Amit".
This example assumes the name begins with the first name and contains a space after it.
Concepts Covered
explode()- Array indexing
- Extracting a name
Question 4: Split Product Codes Using a Hyphen
Problem Statement
A company uses product codes such as "PRD-2026-105". Split the code into its individual parts using a hyphen.
PHP Solution
<?php
$productCode = "PRD-2026-105";
$parts = explode("-", $productCode);
echo "Product Type: " . $parts[0] . "\n";
echo "Year: " . $parts[1] . "\n";
echo "Product ID: " . $parts[2];
?>
Sample Output
Product Type: PRD
Year: 2026
Product ID: 105
Explanation
The hyphen separates the product code into three parts:
- Index
0contains the product type. - Index
1contains the year. - Index
2contains the product ID.
This approach is useful when working with product identifiers that follow a consistent format.
Concepts Covered
- Splitting product codes
explode()- Reading array elements
Question 5: Split a String Into a Limited Number of Parts
Problem Statement
Split "PHP,Java,Python,SQL" into a maximum of three parts using explode().
PHP Solution
<?php
$languages = "PHP,Java,Python,SQL";
$result = explode(",", $languages, 3);
print_r($result);
?>
Sample Output
Array
(
[0] => PHP
[1] => Java
[2] => Python,SQL
)
Explanation
The third argument of explode() specifies the maximum number of array elements.
Because the limit is 3, PHP creates three elements. The remaining text is kept together in the final element.
Concepts Covered
explode()limit parameter- Splitting strings
- Handling remaining text
Question 6: Join Array Elements Into a String Using implode()
Problem Statement
Create an array containing "HTML", "CSS", and "JavaScript". Join the elements into one string separated by commas.
PHP Solution
<?php
$subjects = ["HTML", "CSS", "JavaScript"];
$result = implode(", ", $subjects);
echo $result;
?>
Sample Output
HTML, CSS, JavaScript
Explanation
The implode() function joins array elements into a single string. The separator ", " is placed between each element.
The original array remains unchanged.
Concepts Covered
implode()- Joining array elements
- String separators
Question 7: Generate a Comma-Separated Employee Skills List
Problem Statement
An employee has the following skills: Excel, SQL, Power BI, and Python. Use implode() to display them as a comma-separated list.
PHP Solution
<?php
$skills = ["Excel", "SQL", "Power BI", "Python"];
$skillList = implode(", ", $skills);
echo "Employee Skills: " . $skillList;
?>
Sample Output
Employee Skills: Excel, SQL, Power BI, Python
Explanation
The skills are stored as individual array elements. implode(", ", $skills) joins them into a single readable string.
This is useful when preparing employee profiles, reports, or summary text.
Concepts Covered
implode()- Employee data
- Formatting lists
Question 8: Convert Form Input Text Into an Array
Problem Statement
A form provides a list of product categories as the string "Laptop,Mobile,Tablet". Split the string and display each category on a separate line.
PHP Solution
<?php
$categories = "Laptop,Mobile,Tablet";
$categoryArray = explode(",", $categories);
foreach ($categoryArray as $category) {
echo $category . "\n";
}
?>
Sample Output
Laptop
Mobile
Tablet
Explanation
First, explode() converts the comma-separated text into an array. Then, foreach loops through the array and displays each category on a new line.
If users can enter the data themselves, validate the input before processing it.
Concepts Covered
explode()foreachloop- Processing comma-separated input
Question 9: Convert a CSV-Like String Into a Sentence
Problem Statement
A company stores an employee’s details in an array. Use implode() to display the employee’s name, department, and employee ID in one line separated by a pipe (|).
PHP Solution
<?php
$employee = ["Neha Sharma", "Finance", "EMP105"];
$details = implode(" | ", $employee);
echo $details;
?>
Sample Output
Neha Sharma | Finance | EMP105
Explanation
The array contains three employee details. implode(" | ", $employee) joins them into one string with a pipe separator.
This format can be useful for simple reports or structured text output. It is not a substitute for a proper CSV encoder when values contain delimiters or quotes.
Concepts Covered
implode()- Custom separators
- Formatting employee records
Question 10: Split and Rejoin a Product List
Problem Statement
A store receives product names in one string: "Laptop,Mobile,Tablet". Split the string into an array, then join the elements using " - " as the separator.
PHP Solution
<?php
$products = "Laptop,Mobile,Tablet";
$productArray = explode(",", $products);
$result = implode(" - ", $productArray);
echo $result;
?>
Sample Output
Laptop - Mobile - Tablet
Explanation
This example combines both functions:
explode()splits the original string into an array.implode()joins the array elements into a new string.- The new string uses
" - "between product names.
The original string is not changed.
Concepts Covered
explode()implode()- Converting strings to arrays and back
- Custom separators
Key Takeaways
explode()in PHP converts a string into an array.implode()in PHP joins array elements into a string.- The separator determines where a string is split or what appears between joined elements.
- Array indexes start at
0. - The optional third argument of
explode()limits the number of resulting elements. foreachcan be used to process the array returned byexplode().- Both functions are useful for processing names, product codes, skills, and comma-separated text.
- Validate user-provided data before processing it.
- Use proper CSV-handling functions when working with actual CSV files or values containing commas and quotes.
Frequently Asked Questions
1. What is explode() in PHP?
explode() splits a string into an array based on a specified separator.
2. What is implode() in PHP?
implode() joins array elements into a single string, optionally placing a separator between them.
3. What is the difference between explode() and implode()?
explode() converts a string into an array, while implode() converts an array into a string.
4. Can explode() split a string using a space?
Yes. Use explode(" ", $text) to split a string at space characters.
5. Can implode() use a custom separator?
Yes. You can use separators such as commas, hyphens, spaces, or pipes.
6. What does the third parameter of explode() do?
It sets the maximum number of elements in the resulting array. If the limit is reached, the remaining text is included in the final element.
7. What happens if the separator is not found?
If the separator does not occur in the string, explode() returns an array containing the original string as its only element, provided the limit does not alter that behavior.
8. Can implode() work with an array of numbers?
Yes. PHP converts scalar values such as integers into strings when joining them.
9. Are explode() and implode() case-sensitive?
These functions do not perform case-based matching. explode() searches for the exact separator, including its letter case, while implode() simply inserts the chosen separator between elements.
10. Where are explode() and implode() used in real projects?
They are useful for processing simple comma-separated lists, splitting structured text, formatting employee details, and converting data between string and array formats.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
