📖 Scenario: You are working on a simple PHP script to combine a list of fruits into a single string. This is useful when you want to display items separated by commas or other characters.
🎯 Goal: Build a PHP script that uses implode (also known as join) to combine an array of fruits into a single string separated by commas.
📋 What You'll Learn
Create an array called $fruits with the exact values: 'apple', 'banana', 'cherry', 'date'
Create a variable called $separator and set it to a comma followed by a space: ', '
Use the implode function with $separator and $fruits to create a string called $fruitString
Print the value of $fruitString
💡 Why This Matters
🌍 Real World
Joining array elements into a string is common when displaying lists, creating CSV lines, or formatting output for users.
💼 Career
Understanding how to combine array data into strings is useful for web developers working with PHP to generate dynamic content.
Progress0 / 4 steps
1
Create the fruits array
Create an array called $fruits with these exact values: 'apple', 'banana', 'cherry', 'date'
PHP
Hint
Use square brackets [] to create the array and separate items with commas.
2
Create the separator variable
Create a variable called $separator and set it to the string ', ' (a comma followed by a space).
PHP
Hint
Remember to put the comma and space inside quotes to make it a string.
3
Use implode to join the fruits
Use the implode function with $separator and $fruits to create a string called $fruitString.
PHP
Hint
The implode function takes the separator first, then the array.
4
Print the joined string
Print the value of $fruitString using echo.
PHP
Hint
Use echo to display the string on the screen.
Practice
(1/5)
1. What does the PHP function implode do when used with an array?
easy
A. It splits a string into an array based on a separator.
B. It joins array elements into a single string with a separator.
C. It sorts the array elements alphabetically.
D. It removes duplicate elements from the array.
Solution
Step 1: Understand the purpose of implode
The implode function takes an array and joins its elements into one string.
Step 2: Recognize the role of the separator
You can specify a separator string to put between elements in the resulting string.
Final Answer:
It joins array elements into a single string with a separator. -> Option B
Quick Check:
implode joins array elements [OK]
Hint: Remember: implode combines array items into one string [OK]
Common Mistakes:
Confusing implode with explode (which splits strings)
Thinking implode sorts or filters arrays
Assuming implode returns an array
2. Which of the following is the correct syntax to join array elements with a comma using implode?
easy
A. implode(',', $array);
B. implode($array, ',');
C. implode($array);
D. implode(',', array);
Solution
Step 1: Recall the correct parameter order of implode
The first parameter is the separator string, the second is the array.
Step 2: Check each option's syntax
implode(',', $array); uses implode(',', $array); which is correct syntax.
Final Answer:
implode(',', $array); -> Option A
Quick Check:
Separator first, array second in implode [OK]
Hint: Separator goes first, array second in implode [OK]
D. The separator and array parameters are swapped in implode.
Solution
Step 1: Check parameter order in implode
implode expects separator first, then array second.
Step 2: Analyze given code
Code uses implode($fruits, ', '); which swaps parameters.
Final Answer:
The separator and array parameters are swapped in implode. -> Option D
Quick Check:
Parameter order matters in implode [OK]
Hint: Separator first, array second in implode [OK]
Common Mistakes:
Swapping parameters order
Assuming implode works like explode
Using wrong variable names
5. You have an array $words = ['one', '', 'three', null, 'five'];. You want to join only the non-empty, non-null elements with a comma. Which code correctly does this?
hard
A. $filtered = array_filter($words); echo implode(', ', $filtered);
B. echo implode(', ', $words);
C. $filtered = array_map(trim, $words); echo implode(', ', $filtered);