0
0
PHPprogramming~20 mins

String split and explode in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Split Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code using explode()?
Consider the following PHP code snippet. What will be the output when it runs?
PHP
<?php
$str = "apple,banana,cherry";
$result = explode(",", $str);
print_r($result);
?>
AArray ( [0] => apple [1] => banana [2] => cherry )
Bapple,banana,cherry
CArray ( [0] => apple,banana,cherry )
DError: explode() expects parameter 2 to be string
Attempts:
2 left
💡 Hint
explode() splits a string into an array using the delimiter.
Predict Output
intermediate
2:00remaining
What does this PHP code output when splitting with limit?
What will be the output of this PHP code?
PHP
<?php
$str = "one,two,three,four";
$result = explode(",", $str, 3);
print_r($result);
?>
AArray ( [0] => one [1] => two [2] => three [3] => four )
BArray ( [0] => one [1] => two [2] => three,four )
CArray ( [0] => one,two,three,four )
DArray ( [0] => one [1] => two [2] => three )
Attempts:
2 left
💡 Hint
The third parameter limits the number of pieces returned.
🔧 Debug
advanced
2:00remaining
What error does this PHP code raise?
What error will this PHP code produce when run?
PHP
<?php
$str = "a|b|c";
$result = explode('|');
print_r($result);
?>
AWarning: explode() expects 2 parameters, 1 given
BNULL
CFatal error: Uncaught ArgumentCountError
DArray ( [0] => a [1] => b [2] => c )
Attempts:
2 left
💡 Hint
explode() needs both delimiter and string parameters.
Predict Output
advanced
2:00remaining
What is the output of this PHP code using str_split()?
What will this PHP code output?
PHP
<?php
$str = "hello";
$result = str_split($str, 2);
print_r($result);
?>
AError: str_split() expects parameter 2 to be integer
BArray ( [0] => h [1] => e [2] => l [3] => l [4] => o )
CArray ( [0] => he [1] => ll [2] => o )
DArray ( [0] => hel [1] => lo )
Attempts:
2 left
💡 Hint
str_split splits string into chunks of given length.
🧠 Conceptual
expert
2:00remaining
How many elements are in the array after this explode() call?
Given the code below, how many elements does the resulting array have?
PHP
<?php
$str = "one,,three,,five";
$result = explode(",", $str);
?>
A6
B3
C4
D5
Attempts:
2 left
💡 Hint
explode() includes empty strings between consecutive delimiters.