Bird
Raised Fist0
PHPprogramming~10 mins

String split and explode in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - String split and explode
Start with string
↓
Choose delimiter
↓
Call explode(delimiter, string)
↓
Split string at each delimiter
↓
Create array of substrings
↓
Return array
↓
End
The string is split into parts wherever the delimiter appears, creating an array of substrings.
Execution Sample
PHP
<?php
$str = "apple,banana,cherry";
$arr = explode(",", $str);
print_r($arr);
?>
This code splits the string by commas and prints the resulting array.
Execution Table
StepActionInput StringDelimiterResulting Array
1Start with stringapple,banana,cherry,N/A
2Call explode functionapple,banana,cherry,N/A
3Split string at each commaapple,banana,cherry,["apple", "banana", "cherry"]
4Return arrayapple,banana,cherry,["apple", "banana", "cherry"]
💡 All parts split by delimiter are collected into the array and returned.
Variable Tracker
VariableStartAfter explodeFinal
$str"apple,banana,cherry""apple,banana,cherry""apple,banana,cherry"
$arrundefinedarray("apple", "banana", "cherry")array("apple", "banana", "cherry")
Key Moments - 3 Insights
Why does explode return an array instead of a string?
Because explode splits the original string into multiple parts at each delimiter, it returns all parts as an array, not a single string. See execution_table step 3.
What happens if the delimiter is not found in the string?
If the delimiter is not found, explode returns an array with the whole string as the only element. This is because no splitting occurs.
Can the delimiter be more than one character?
Yes, the delimiter can be multiple characters, and explode will split wherever that exact sequence appears.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the resulting array after step 3?
A["apple", "banana"]
B["apple", "banana", "cherry"]
C["apple,banana,cherry"]
D[]
💡 Hint
Check the 'Resulting Array' column in step 3 of the execution_table.
According to variable_tracker, what is the value of $arr before calling explode?
A"apple,banana,cherry"
Barray("apple", "banana", "cherry")
Cundefined
Dnull
💡 Hint
Look at the 'Start' column for $arr in variable_tracker.
If the delimiter was changed to ";" and the string stayed the same, what would $arr contain?
A["apple,banana,cherry"]
B["apple", "banana", "cherry"]
C[]
D[""]
💡 Hint
If delimiter not found, explode returns array with whole string as one element.
Concept Snapshot
explode(delimiter, string) splits string at each delimiter occurrence
Returns an array of substrings
If delimiter not found, returns array with original string
Delimiter can be multiple characters
Useful to break strings into parts easily
Full Transcript
This example shows how the PHP function explode splits a string into an array using a delimiter. Starting with the string 'apple,banana,cherry', we call explode with ',' as the delimiter. The function finds each comma and splits the string into parts: 'apple', 'banana', and 'cherry'. These parts are collected into an array and returned. The variable $arr holds this array after explode runs. If the delimiter is not found, explode returns an array with the whole string as one element. The delimiter can be more than one character. This process helps break strings into manageable pieces for further use.

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

  1. Step 1: Understand the purpose of explode()

    The explode() function takes a string and splits it into pieces based on a delimiter.
  2. Step 2: Recognize the output type

    It returns an array containing the split parts of the original string.
  3. Final Answer:

    It splits a string into an array using a delimiter. -> Option D
  4. 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

  1. Step 1: Identify explode() parameter order

    The first parameter is the delimiter (separator), the second is the string to split.
  2. 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').
  3. Final Answer:

    explode(',', 'apple,banana,orange') -> Option C
  4. Quick Check:

    explode(delimiter, string) = explode(',', 'apple,banana,orange') [OK]
Hint: Delimiter is first, string second in explode() [OK]
Common Mistakes:
  • Swapping the order of parameters
  • Using wrong delimiter like space instead of comma
  • Passing parts of string as delimiter
3. What is the output of the following PHP code?
$str = "one,two,three,four";
$result = explode(",", $str, 3);
print_r($result);
medium
A. Array ( [0] => one [1] => two [2] => three,four )
B. Array ( [0] => one [1] => two [2] => three )
C. Array ( [0] => one,two [1] => three [2] => four )
D. Array ( [0] => one [1] => two [2] => three [3] => four )

Solution

  1. Step 1: Understand explode() with limit parameter

    The third parameter limits the number of pieces. Here, limit=3 means maximum 3 parts.
  2. Step 2: Apply limit to splitting

    The string splits at first two commas: 'one', 'two', and the rest 'three,four' stays as last part.
  3. Final Answer:

    Array ( [0] => one [1] => two [2] => three,four ) -> Option A
  4. Quick Check:

    explode(',', string, 3) limits to 3 parts [OK]
Hint: Limit splits into max parts; last part keeps rest [OK]
Common Mistakes:
  • Ignoring the limit parameter effect
  • Expecting all parts split regardless of limit
  • Confusing array keys or values
4. Identify the error in this PHP code snippet:
$data = "red|green|blue";
$colors = explode($data, "|");
print_r($colors);
medium
A. The delimiter "|" is invalid in explode().
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

  1. Step 1: Check explode() parameter order

    explode() expects delimiter first, then the string to split.
  2. Step 2: Analyze given code

    Code uses explode($data, "|"), which swaps parameters incorrectly.
  3. Final Answer:

    The delimiter and string parameters are swapped in explode(). -> Option B
  4. 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

  1. 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.
  2. Step 2: Apply to given string

    With limit=3, parts are: 'name', 'age', and 'city:country' combined as last part.
  3. Final Answer:

    explode(':', $string, 3) -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard