The explode() function splits a string into an array using a specified delimiter.
Click to reveal answer
beginner
How do you split a string by spaces using explode()?
Use explode(' ', $string) to split the string at each space character.
Click to reveal answer
intermediate
What is the difference between explode() and str_split()?
explode() splits a string by a delimiter into an array of substrings. str_split() splits a string into an array of single characters or fixed-length chunks.
Click to reveal answer
beginner
What will explode(',', 'apple,banana,orange') return?
It returns an array: ['apple', 'banana', 'orange'] splitting the string at each comma.
Click to reveal answer
intermediate
Can explode() handle an empty delimiter?
No, explode() requires a non-empty string as the delimiter. Passing an empty string will cause a warning.
Click to reveal answer
Which function splits a string into an array by a delimiter in PHP?
Asubstr()
Bimplode()
Cstr_replace()
Dexplode()
✗ Incorrect
explode() splits strings by a delimiter. implode() joins array elements into a string.
What does explode('-', '2024-06-15') return?
A['2024', '06', '15']
B'2024-06-15'
C['2024-06-15']
D['2024', '-06', '-15']
✗ Incorrect
The string is split at each dash, producing an array of three parts.
What happens if the delimiter is not found in the string when using explode()?
AThrows an error
BReturns an empty array
CReturns an array with the original string as the only element
DReturns null
✗ Incorrect
If delimiter is missing, explode() returns an array containing the whole string.
Which function splits a string into single characters or fixed-length chunks?
Astr_split()
Bexplode()
Csubstr()
Dimplode()
✗ Incorrect
str_split() splits strings into characters or chunks, unlike explode() which uses delimiters.
What is the correct way to split a CSV string into an array?
Aexplode(' ', $csvString)
Bexplode(',', $csvString)
Cstr_split($csvString)
Dimplode(',', $csvString)
✗ Incorrect
CSV values are separated by commas, so use explode(',', $csvString).
Explain how the explode() function works and give an example.
Think about how you cut a sentence into words using spaces.
You got /4 concepts.
Describe the difference between explode() and str_split() in PHP.
One splits by a character or string, the other by length.
You got /3 concepts.
Practice
(1/5)
1. What does the PHP function explode() do?
easy
A. It replaces parts of a string with another string.
B. It joins array elements into a string.
C. It counts the number of characters in a string.
D. It splits a string into an array using a delimiter.
Solution
Step 1: Understand the purpose of explode()
The explode() function takes a string and splits it into pieces based on a delimiter.
Step 2: Recognize the output type
It returns an array containing the split parts of the original string.
Final Answer:
It splits a string into an array using a delimiter. -> Option D
Quick Check:
explode() = split string to array [OK]
Hint: explode() breaks strings into arrays by delimiter [OK]
Common Mistakes:
Confusing explode() with implode() which joins arrays
Thinking explode() returns a string instead of an array
Assuming explode() modifies the original string
2. Which of the following is the correct syntax to split the string 'apple,banana,orange' by commas using explode()?
easy
A. explode(' ', 'apple,banana,orange')
B. explode('apple,banana,orange', ',')
C. explode(',', 'apple,banana,orange')
D. explode('apple', 'banana,orange')
Solution
Step 1: Identify explode() parameter order
The first parameter is the delimiter (separator), the second is the string to split.
Step 2: Match parameters to the example
To split by commas, delimiter is ',' and string is 'apple,banana,orange'. So syntax is explode(',', 'apple,banana,orange').
B. The delimiter and string parameters are swapped in explode().
C. explode() cannot split strings with pipe characters.
D. print_r() cannot print arrays.
Solution
Step 1: Check explode() parameter order
explode() expects delimiter first, then the string to split.
Step 2: Analyze given code
Code uses explode($data, "|"), which swaps parameters incorrectly.
Final Answer:
The delimiter and string parameters are swapped in explode(). -> Option B
Quick Check:
explode(delimiter, string) correct order [OK]
Hint: Delimiter always first, string second in explode() [OK]
Common Mistakes:
Swapping parameters causing wrong output or errors
Thinking delimiter must be a letter only
Assuming print_r() can't print arrays
5. You have a string "name:age:city:country". You want to split it into exactly 3 parts where the last part contains everything after the second colon. Which explode() call achieves this?
hard
A. explode(':', $string, 3)
B. explode(':', $string, 2)
C. explode(':', $string, 4)
D. explode(':', $string)
Solution
Step 1: Understand the limit parameter in explode()
Limit defines max number of parts. If limit=3, explode splits at first two delimiters, last part has rest.
Step 2: Apply to given string
With limit=3, parts are: 'name', 'age', and 'city:country' combined as last part.
Final Answer:
explode(':', $string, 3) -> Option A
Quick Check:
Limit 3 splits into 3 parts, last part keeps rest [OK]
Hint: Use limit=3 to get 3 parts, last part keeps remaining string [OK]
Common Mistakes:
Using limit=2 which gives only 2 parts
Using no limit which splits all parts
Confusing limit with number of delimiters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.