0
0
PHPprogramming~10 mins

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

Choose your learning style9 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.